0

This is the class of java scripts. I get the error of undefined for GetL() and its code doesn't run (I tried random consoles inside and they never get run). The first part of the function shows the array but GetL shows undefined.

What am I doing wrong?

function getC() {
    var url = config.api.server + config.api.uri + "/c";
    $.getJSON(url).done(function(r) {
      if (!r.length) {
        console.warn("Empty");
      }
      config.c = r;
      console.log(r);
      $.map(config.c, function(item) {

        $("#c").append($('<option>').text(item.ci));

      });

      var a = $("#c").find("option:selected").text();


      function GetL() {

        var url = config.api.server + config.api.uri + "/l";

        $.getJSON(url).done(function(answer) {

          if (!answer.length) {
            console.warn("Empty list");
          }
          config.l = answer;
          console.log(answer);

          r.forEach(function(cName) {
            if (a == cName.ci) //check if name is equal to selection 
            {
              var x = cName.ci.l; //get the ls of that name and put it in a place holder 
              console.log(x); //undefined
              $.map(x, function(item) { //make a map to append those specific ls

                $("#l").append($('<option>').text(item.str));
              });
            }

          });

        }).fail(function(data, status, error) {
          console.error("Something went wrong");
        });

      }

      GetL();
    }).fail(function(data, status, error) {
      console.error("Something went wrong");
    });


  } //end big function
8
  • 1
    Eyeballing that, I can't see any reason why GetL would be undefined. What is the exact error message you get? Commented Nov 29, 2016 at 10:19
  • GetL does not return anything to begin with. Furthermore, $.getJSON is asynchronous. I'm not totally sure what your expectation is. Commented Nov 29, 2016 at 10:20
  • 1
    By "the array" to you mean a or something else? (A shorter re-create that removes everything not needed to show the issue would make answering easier.) Commented Nov 29, 2016 at 10:20
  • Could you console.log the array r at the begining of your GetL function, and the item cName next var x = cName.ci.l; ? Commented Nov 29, 2016 at 10:36
  • Could you please include the exact error message in your question? Commented Nov 29, 2016 at 11:13

1 Answer 1

1

You should be returning the values that you are looking for.

return x

you can do

console.log(x)
return x

just to make sure the values match.

Sign up to request clarification or add additional context in comments.

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.