简体   繁体   English

解析云代码循环仅通过第一个实例

[英]Parse cloud code loop is only going through first instance

I want the following loop to go through every instance of matchCenterItem , yet for some reason, it pings ebay using the properties of only the first instance. 我希望以下循环遍历matchCenterItem每个实例,但是由于某些原因,它仅使用第一个实例的属性ping ebay。 The console logs at the end of the function however, loop through all instances and log their respective properties. 控制台在功能末尾记录日志,循环遍历所有实例并记录它们各自的属性。

  Parse.Cloud.define("MatchCenterTest", function(request, response) {

  var matchCenterItem = Parse.Object.extend("matchCenterItem");
  var query = new Parse.Query(matchCenterItem);

  var promises = [];

  query.limit(10);
  query.find().then(function(results) {
    for (i=0; i<results.length; i++) {

      url = 'http://svcs.ebay.com/services/search/FindingService/v1';
       promises.push(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'),
        },
        // success: function (httpResponse) {
        //   // parses results
        //   var httpresponse = JSON.parse(httpResponse.text);

        //   response.success(httpresponse);
        //   console.log('MatchCenter Pinged eBay dude!');
        // },
        // error: function (httpResponse) {
        //   console.log('error!!!');
        //   response.error('Request failed with response code ' + httpResponse.status);
        // }
      }));

     console.log(results[i].get('itemCondition'));
     console.log(results[i].get('maxPrice'));
     console.log(results[i].get('minPrice'));
     console.log(results[i].get('searchTerm'));

    }
  });

Parse.Promise.when(promises).then(function(results) {

   var httpresponse = JSON.parse(httpResponse.text);
   response.success(httpresponse);
}, function(err) {
    console.log('error!!!');
});



});

This is because the http request is asynchronous, and you're calling response.success in the completion handler for the first (and all) requests. 这是因为http请求是异步的,并且您在完成处理程序中为第一个(和所有)请求调用response.success。 Use the promise syntax and only complete when they are done. 使用promise语法,仅在完成时完成。 Simplified concept: 简化概念:

var promises = [];
for (...) {
   promises.push(Parse.Cloud.httpRequest({...}));  // no success/error params
}
Parse.Promise.when(promises).then(function(results) {
   response.success(...);
}, function(err) {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM