Javascript help

randomcat

Expert Member
Joined
Dec 15, 2018
Messages
3,243
Reaction score
4,031
Location
A box
I need a little help. Getting error obj is undefined.

HTML:
<!DOCTYPE html>
<html>
<head>
    <title>rest</title>
</head>
<body>
    <ul id="myList">
    </ul>
<script>
var obj;

function fetchdata(){
fetch('https://restcountries.eu/rest/v2/?fullText=true')
  .then(resp=> resp.json())
  .then(success => obj = success);
}

fetchdata();



var obLength = obj.length;
    for (i = 0; i < obLength; i++){
      var nm = obj[i].name;

      if (nm.startsWith("A") && nm.endsWith("a")){
      var x = document.createElement("LI");
      var t = document.createTextNode(obj[i].name);
      x.appendChild(t);
      document.getElementById("myList").appendChild(x);
      }
    }
</script>
</body>
</html>
 
Maybe try changing this:
.then(resp=> resp.json())
to:
.then(resp=> obj = resp.json())
 
The link to fetch the data isn't working so you won't receive any data. You'll also need to place the result handling logic in a function then pass the response as a parameter.

I don't have time to test it but something like this:
Code:
var obj;

function fetchdata(){
fetch('https://restcountries.eu/rest/v2/?fullText=true')
  .then(resp=> handleResponse(resp.json()))
  .then(success => obj = success);
}

fetchdata();


function handleResponse(response) {
var obLength = response.length;
    for (i = 0; i < obLength; i++){
      var nm = response[i].name;

      if (nm.startsWith("A") && nm.endsWith("a")){
      var x = document.createElement("LI");
      var t = document.createTextNode(response[i].name);
      x.appendChild(t);
      document.getElementById("myList").appendChild(x);
      }
    }
}
 
Did you check if that route you are grabbing the data from works?
Im getting a 521 error(Web Server Down)

Download Postman to test API's and their endpoints before using them - comes in handy
 
Fetch is async, so it will continue to "var obLength = obj.length;" before the call has returned.
You want to call that part from a "then", the same one where you set obj = success.
 
Last edited:
Did you check if that route you are grabbing the data from works?
Im getting a 521 error(Web Server Down)

Download Postman to test API's and their endpoints before using them - comes in handy
It's was working earlier but it's down again. I'm not using it in any production code.The company sent me the link. It's suppose to be a test(I'm not doing great so far).

Fetch is async, so it will continue to "var obLength = obj.length;" before the call has returned.
You want to call that part from a "then", either the same one where you set obj = success, or to its own "then".
Thanks I will try it.
 
It's was working earlier but it's down again. I'm not using it in any production code.The company sent me the link. It's suppose to be a test(I'm not doing great so far).


Thanks I will try it.

Notify the company/person who is testing you that the link is not working or that there are stability issues
 
Last edited:
It's was working earlier but it's down again. I'm not using it in any production code.The company sent me the link. It's suppose to be a test(I'm not doing great so far).


Thanks I will try it.

I edited my response a bit, you can't just add another .then().
 
Thanks for the help and advice.

HTML:
<!DOCTYPE html>
<html>
<head>
    <title>rest</title>
</head>
<body>
    <ul id="myList">
    </ul>
<script>



async function fetchCountry() {
    //api call
  const response = await fetch('https://restcountries.eu/rest/v2/?fullText=true')
  const data = await response.json();
  //looping through data to find specific countries
  for (i = 0; i < data.length; i++){
  var nm = data[i].name;
if (nm.startsWith("A") && nm.endsWith("a")){
    //append html
      var x = document.createElement("LI");
      var t = document.createTextNode(data[i].name);
      x.appendChild(t);
      document.getElementById("myList").appendChild(x);
    }
  }
}

//calling function
fetchCountry();

</script>
</body>
</html>
 
Top
Sign up to the MyBroadband newsletter
X