0

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. 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!!!');
});



});

1 Answer 1

3

This is because the http request is asynchronous, and you're calling response.success in the completion handler for the first (and all) requests. Use the promise syntax and only complete when they are done. 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) {

});
Sign up to request clarification or add additional context in comments.

3 Comments

The logic makes sense, however, when adding Parse.Promise.when(promises).then(function(results) { var httpresponse = JSON.parse(httpResponse.text); response.success(httpresponse); }, function(err) { console.log('error!!!'); }); after the for loop, I'm getting an error stating that httpresponse isn't defined. Any idea why?
I've edited my code in the question to reflect how it now looks.
You'll need to loop through the results variable, and access the response inside. I.e results[0].text

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.