我正在尝试将Foursquare场地请求的结果添加到KO Observable数组中。我无法解决推送未定义的错误。我假设这是因为帮助程序没有适当地处理数据。
我定义数组并发出请求
storelocations = ko.observableArray([]);
var fourquarerequest = $.ajax({
async: true,
url: 'https://api.foursquare.com/v2/venues/search',
dataType: 'json',
data: 'XXXXrequestXXXXX',
})
然后我尝试遍历结果,使用帮助程序处理它们,并将每个结果添加到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))
});
这是处理
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 = "";
}
};
答案 0 :(得分: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()
.