我正在使用一个承诺为matchCenterItem
的每个实例创建httpRequest函数,将它们推入一个数组,然后连续运行它们。我想要做的是收集每个函数的httpResponse,并将它们推送到ebayPingResults
数组。当我运行这个时,我收到一个错误,说明以下内容:
Failed with: TypeError: Cannot read property 'text' of undefined
at collectEbayResults (main.js:444:57)
at null.<anonymous> (main.js:450:15)
at e (Parse.js:2:5101)
at c.extend.then (Parse.js:2:5447)
at main.js:439:40
at e (Parse.js:2:5101)
at Parse.js:2:4651
at Array.forEach (native)
at Object.x.each.x.forEach [as _arrayEach] (Parse.js:1:665)
at c.extend.resolve (Parse.js:2:4602)
它指的是这样的:
var httpresponse = JSON.parse(httpResponse.text);
这是完整的代码:
Parse.Cloud.define("MatchCenterTest", function(request, response) {
//defines which parse class to iterate through
var matchCenterItem = Parse.Object.extend("matchCenterItem");
var query = new Parse.Query(matchCenterItem);
var promises = [];
//setting the limit of items at 10 for now
query.limit(10);
query.find().then(function(results) {
for (i=0; i<results.length; i++) {
console.log(results[i].get('searchTerm'))
url = 'http://svcs.ebay.com/services/search/FindingService/v1';
//push function containing criteria for every matchCenterItem into promises array
promises.push(function() {
return Parse.Cloud.httpRequest({
url: url,
params: {
'OPERATION-NAME' : 'findItemsByKeywords',
'SERVICE-VERSION' : '1.12.0',
'SECURITY-APPNAME' : '*APP ID GOES HERE*',
'GLOBAL-ID' : 'EBAY-US',
'RESPONSE-DATA-FORMAT' : 'JSON',
'REST-PAYLOAD&sortOrder' : 'BestMatch',
'paginationInput.entriesPerPage' : '3',
'outputSelector=AspectHistogram&itemFilter(0).name=Condition&itemFilter(0).value(0)' : results[i].get('itemCondition'),
'itemFilter(1).name=MaxPrice&itemFilter(1).value' : results[i].get('maxPrice'),
'itemFilter(1).paramName=Currency&itemFilter(1).paramValue' : 'USD',
'itemFilter(2).name=MinPrice&itemFilter(2).value' : results[i].get('minPrice'),
'itemFilter(2).paramName=Currency&itemFilter(2).paramValue' : 'USD',
//'itemFilter(3).name=LocatedIn&itemFilter(3).Value' : request.params.itemLocation,
'itemFilter(3).name=ListingType&itemFilter(3).value' : 'FixedPrice',
'keywords' : results[i].get('searchTerm'),
}
});
});
}
console.log(promises);
Parse.Promise.when(promises).then(function(results){
var ebayPingResults = [];
function collectEbayResults (httpResponse){
var httpresponse = JSON.parse(httpResponse.text);
ebayPingResults.push(httpresponse);
}
for (i=0; i<promises.length; i++)
{
collectEbayResults(httpresponse);
}
console.log(ebayPingResults);
response.success(
{
"Top 3": ebayPingResults
}
);
}, function(err) {
console.log('error!');
response.error('DAMN IT MAN');
});
});
});
我已经编写了一个以这种方式为成功函数引用httpResponse的前一个函数,它工作正常,不确定为什么它在这种情况下不起作用。