0

I'm trying to add the results of a Foursquare venues request to a KO Observable array. I can't solve the error of push being undefined. I assume this is because of the helper not processing the data appropriately.

I define the array and make the request

storelocations = ko.observableArray([]);

  var fourquarerequest = $.ajax({
        async: true,
        url: 'https://api.foursquare.com/v2/venues/search',
        dataType: 'json',
        data: 'XXXXrequestXXXXX',
    })

Then I attempt to go through the results, process them with a helper, and add each to the Observable Array

            fourquarerequest.done((response) => {

            var self = this
                data = response.response.venues;

                for (var name in data) {
                  shop = new shopdetails(data);
                  self.storelocations.push(shop);
                  console.log(storelocations);
                  }

                //console.log(JSON.stringify(data))
            });

And here's the processing

  var shopdetails = function (shop) {
    var self = this;
    self.name = data.name;
    self.lat = data.lat;
    self.long = data.long;
    self.URL = data.url;
      if (typeof self.URL === 'undefined') {
          self.URL = "";
        }
  };
2
  • Seems to be a scope problem. Where exactly have you defined storelocations = ko.observableArray([])? Please show that as well in the code
    – Ray
    Mar 22, 2018 at 22:59
  • sorry, the Stack Overflow formatting wasn't correct. It's fixed now, storelocations is defined above the ajax call
    – Justin M
    Mar 22, 2018 at 23:37

1 Answer 1

0

It´s a problem of scope. You have defined self inside the foursquare.done function and then written self.storedlocations.push(), even though storedlocations is a global variable.

Change it to storedlocations.push().

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.