Search code examples
javascriptparse-platformebay-api

eBayResults array showing up blank


My Parse background job is pinging eBay's API with certain criteria, and then doing things with the resulting JSON. This resulting JSON is httpResponse.text, which I represent with the parameter eBayResponseText in the buildEbayRequestPromises function.

eBayResults is an array of top3 arrays. I know that eBayResponseText is valid and correct JSON, because when I console.log it at the beginning of buildEbayRequestPromises, it prints out the eBay JSON that I expect.

However, for some reason, when I console.log(eBayResults) in the matchCenterComparison function, it just comes out as , , , rather than the 4 top3 arrays I'm expecting.

Because of this, when I attempt to save a Parse object to a user's account with eBayResults as a property, the property shows up as [null,null,null,null]. I have no idea why it's not reading the eBayResponseText properly. Is there some error in how I'm parsing the JSON?

Function that builds eBayResults array:

/* Previous code is asynchronously pinging eBay API, and the below code runs once 
that is done */

    .then(function() {
            // process promises, return query promise
            return Parse.Promise.when(shared.promises).then(function() {
                // process the results of the promises, returning a query promise
          console.log('were in the when.then of promise');

          var eBayResults = [];
          for (var i = 0; i < arguments.length; i++) {
          var httpResponse = arguments[i];
          // since they're in the same order, this is OK:
          var searchTerm = shared.searchTerms[i];
          // pass it as a param:
          var top3 = buildEbayRequestPromises(httpResponse.text, searchTerm);

          eBayResults.push(top3);
          }

          return eBayResults;
            });
     });

buildEbayRequestPromises:

// process matchCenterItem results to build eBay promises
function buildEbayRequestPromises(eBayResponseText, shared) {
    // ... code that pushes items into shared.promises and shared.searchTerms ...

  console.log('so heres what the ebayresponsetext is:' + eBayResponseText);      

  var ebayResponse = JSON.parse(eBayResponseText);
  var matchCenterItems = [];

  //Parses through ebay's response, pushes each individual item and its properties into an array  
  ebayResponse.findItemsByKeywordsResponse.forEach(function(itemByKeywordsResponse) {
    itemByKeywordsResponse.searchResult.forEach(function(result) {
      result.item.forEach(function(item) {
        matchCenterItems.push(item);
      });
    });
  });

  var top3Titles = [];
  var top3Prices = [];
  var top3ImgURLS = [];
  var top3ItemURLS = [];

  //where the title, price, and img url are sent over to the app
  matchCenterItems.forEach(function(item) {
    var title = item.title[0];
    var price = item.sellingStatus[0].convertedCurrentPrice[0].__value__;
    var imgURL = item.galleryURL[0];
    var itemURL = item.viewItemURL[0];

    top3Titles.push(title);
    top3Prices.push(price);
    top3ImgURLS.push(imgURL);
    top3ItemURLS.push(itemURL);
  });

  console.log('about to define top3 value');

  var top3 = 
    {
        "Top 3": 
            [
              {
              "Title": top3Titles[0],
              "Price": top3Prices[0],
              "Image URL": top3ImgURLS[0],
              "Item URL": top3ItemURLS[0]
              },

              {
                "Title": top3Titles[1],
                "Price": top3Prices[1],
                "Image URL": top3ImgURLS[1],
                "Item URL": top3ItemURLS[1]
              },

              {
                "Title": top3Titles[2],
                "Price": top3Prices[2],
                "Image URL": top3ImgURLS[2],
                "Item URL": top3ItemURLS[2]
              }
            ]
    };

}

matchCenterComparison:

function matchCenterComparison(eBayResults) {   

    console.log('eBayResults are the following:' + eBayResults);

    //rest of the code snipped out for the sake of space in this post     
}

Solution

  • In your buildEbayRequestPromises() function, you are creating a variable called top3, but not actually returning it from the function to the calling code.

    // at the end of the function
    return top3;
    

    That would actually "return" or pass back the value to the calling code... it is not enough to just name things the same, they have different scope.

    If you're not familiar with variable scoping, I would suggest reading up on it, as it is a key thing to understand.