eBayResults数组显示空白

时间:2014-08-04 00:46:47

标签: javascript parse-platform ebay

我的Parse后台工作是用某些标准ping eBay的API,然后用生成的JSON做事。生成的JSON为httpResponse.text,我在eBayResponseText函数中使用参数buildEbayRequestPromises表示。

eBayResultstop3数组的数组。我知道eBayResponseText是有效且正确的JSON,因为当我在buildEbayRequestPromises的开头调度它时,它打印出我期望的所有eBay JSON。

但是,出于某种原因,当我在eBayResults函数中的console.log matchCenterComparison时,它只是, , ,而不是4 top3数组我我期待着。

因此,当我尝试将Parse对象保存到以eBayResults作为属性的用户帐户时,该属性显示为[null,null,null,null]。我不知道为什么它没有正确阅读eBayResponseText。我是如何解析JSON的?

构建eBayResults数组的函数:

/* 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     
}

1 个答案:

答案 0 :(得分:1)

buildEbayRequestPromises()函数中,您正在创建一个名为top3的变量,但实际上并没有将它从函数返回到调用代码。

// at the end of the function
return top3;

那实际上是&#34;返回&#34;或者将值传回给调用代码......仅仅命名相同的东西是不够的,它们具有不同的范围。

如果您不熟悉变量范围,我建议您阅读它,因为这是理解的关键。

相关问题