【问题标题】:eBayResults array showing up blankeBayResults 数组显示为空白
【发布时间】:2014-08-04 00:46:47
【问题描述】:

我的 Parse 后台工作是使用特定标准 ping eBay 的 API,然后使用生成的 JSON 执行操作。这个生成的 JSON 是 httpResponse.text,我在 buildEbayRequestPromises 函数中用参数 eBayResponseText 表示。

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

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

因此,当我尝试使用 eBayResults 作为属性将 Parse 对象保存到用户帐户时,该属性显示为 [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]
              }
            ]
    };

}

ma​​tchCenterComparison:

function matchCenterComparison(eBayResults) {   

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

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

【问题讨论】:

    标签: javascript parse-platform ebay-api


    【解决方案1】:

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

    // at the end of the function
    return top3;
    

    这实际上会“返回”或将值传回给调用代码...仅仅命名相同是不够的,它们有不同的范围。

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

    【讨论】:

      猜你喜欢
      • 2019-03-26
      • 2018-01-23
      • 2015-04-14
      • 2022-01-09
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多