2

I have array created from JSON, and I want to create new options in my select. I do not have any errors, and I don't know what's wrong with my code. In HTML I have:

<select class="currencyList">
</select>

And in JS:

var currencyList = [];

    $(document).ready(function() {
      getCurrencies();
      createOptions();
    });

    function getCurrencies() {
      $.getJSON(
        "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
        function(response) {
          $.each(response[0].rates, function(i, item) {
            currencyList.push(item);
          });
        }
      );
    }

    function createOptions() {
      var option = "";
      for (var i = 0; i < currencyList.length; i++) {
        option += "<option value='" + currencyList[i].code + "'>" + currencyList[i].currency + "</option>";
      }
        $(".currencyList").append(option);
    }

I can access data in the array from the console.

1
  • getCurrencies() is asynchronous, so createOptions() is called immediately after getCurrencies(), which means currencyList more than likely will not be populated yet. Commented May 2, 2018 at 13:43

4 Answers 4

2

I don't know what's wrong with my code

That's because the $.getJSON is an async function.

Basically, you're invoking createOptions function before the $.getJSON AJAX call is done.

You need to attach done promise callback function.

function getCurrencies() {
  $.getJSON(
    "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
    function(response) {
      $.each(response[0].rates, function(i, item) {
        currencyList.push(item);
      });
    }
  ).done(function(){
       createOptions();
  });
}
Sign up to request clarification or add additional context in comments.

Comments

1

getCurrencies is an asynchronous call, so currencyList is still empty when createOptions is called.

Call createOptions(); after each

$(document).ready(function() {
  getCurrencies();      
});

function getCurrencies() {
  $.getJSON(
    "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
    function(response) {
      $.each(response[0].rates, function(i, item) {
        currencyList.push(item);
      });
      createOptions();//call here
    }
  );
}

1 Comment

Thanks for answer, now i understand my mistake!
0

Because the $.getJSON is async function, you should call createOptions() into it not into your document ready function:

function getCurrencies() {
      $.getJSON(
        "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
        function(response) {
          $.each(response[0].rates, function(i, item) {
            currencyList.push(item);
          });
          createOptions();
        }
      );
    }

Comments

0

You are using an AJAX Request and correctly build the currencyList upon the return of the call but you are calling createOptions before the AJAX call is done.

That is because the code will continue executing, calling getCurrency and createOptions right away.

You can call createOptions from the getCurrency success directly, similar to the below, passing the currency list into it:

function getCurrencies() {
  $.getJSON(
    "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
    function(response) {
      var currencyList;

      $.each(response[0].rates, function(i, item) {
        currencyList.push(item);
      });

      createOptions(currencyList)
    }
  );
}

function createOptions(currencyList) {
  var option = "";
  for (var i = 0; i < currencyList.length; i++) {
    option += "<option value='" + currencyList[i].code + "'>" + currencyList[i].currency + "</option>";
  }
  $(".currencyList").append(option);
}

You can also just process the currency more directly as it comes in, similar to this:

function getCurrencies() {
  $.getJSON(
    "http://api.nbp.pl/api/exchangerates/tables/a/?format=json",
    createList
  );
}

function createList(response) {
  var $list = $(".currencyList");
  var option;

  $.each(response[0].rates, function(i) {
    option = "<option value='" + response[0].rates[i].code + "'>" + response[0].rates[i].currency + "</option>"

    $list.append(option);
  });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.