2

Javascript: I have a dynamically filled array named resultsAuthor, when I log this object, I get this. (logged it because I need the length to loop through the object and this wasn't working) 1 But when I try to log resultsAuthor.length, I get 0. Anyone know what I'm doing wrong?

This is in my utils.js

From there I call it in my main.js like this:

var searchCriteria = document.querySelector('[title="tbxZoeken"]').value;
var searchAuthor = new Utils.GetBookinfoByAuthor(searchCriteria);
var resultsAuthor = searchAuthor.loadData();

console.log(resultsAuthor); // returns the object
console.log(resultsAuthor.length); // returns 0

Thanks!

Annelien
  • 31
  • 5
  • 2
    Provide your code, can't help otherwise. You're probably not reading the good var or maybe you've deleted the entries in between... – Quentin Morrier Jul 31 '17 at 17:34
  • Show us your code, a jsfiddle too if you have it. – Jacksonkr Jul 31 '17 at 17:35
  • My guess is you're filling the array from an AJAX process but trying to log the length of the array before the promised has returned the data. Edit: Hey! Looks like I was right... :) – Andy Jul 31 '17 at 17:41
  • 1
    https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Andy Jul 31 '17 at 17:45
  • Thanks Andy! Seems like this could be my problem. I am pretty new to all of this, so I'm having a hard time fixing it, even with the options provided in the link you send me. If you have an easy solution for me, you would be my hero, been searching for a solution for over 3 hours now... – Annelien Jul 31 '17 at 18:53
  • Good duplicate mark. Man, Chrome can be so stupid sometimes. – Patrick Roberts Jul 31 '17 at 18:59
  • Hey Patrick, I don't really get it... So the problem is with my browser? – Annelien Jul 31 '17 at 19:04

1 Answers1

1

Your ajax call has probably not finished loading when you print resultsAuthor.length, so it return 0. If you print resultAuthor this is still a reference to the original variable, so it gets printed correctly.

To fix this you can use a callback function or a promise. With a callback you could do something like this:

In your utils.js

GetBookinfoByAuthor: function (auteur) {
    //...
    this.loadData = function(callback) {

       // Rest of your code...

       // Request returned sucessfully
       if (xhr.status === 200) {

          //Rest of your code....               

          for (var i = 0; i  < data.length; i++) {
            // add all books to array
          }

       // All books a loaded
       callback(books)
    }
   }
}

In your main.js

var searchCriteria = document.querySelector('[title="tbxZoeken"]').value;
var searchAuthor = new Utils.GetBookinfoByAuthor(searchCriteria);
searchAuthor.loadData(function() {
  console.log(resultsAuthor);
  console.log(resultsAuthor.length);
});
Nils Schlüter
  • 1,418
  • 1
  • 19
  • 30
  • Thanks for your reply, When I add this to my code I get an error saying 'callback is not a function at XMLHttpRequest.xhr.onload' And should my resultsAuthor.length work when I add this callback to my code? This is all pretty new for me so I'm having a hard time understanding all this... – Annelien Jul 31 '17 at 18:42