javascript - 在 Xcode 中收到引用错误,指出 undefined object 。

标签 javascript ios objective-c json parse-platform

我正在编写解析云代码,用于 ping eBay,返回带有项目结果的 JSON 数据,然后解析该数据并将前两个类别存储到数组中。发送到 eBay 的查询基于用户在我的 iOS 应用程序的 itemSearch 栏中输入的任何内容。当我尝试发送“iPhone”之类的查询时,它给我一个错误,说明如下:

ReferenceError: data is not defined
at Object.Parse.Cloud.httpRequest.success (main.js:34:11)
at Object.<anonymous> (<anonymous>:565:19) (Code: 141, Version: 1.2.18)

objective-c代码如下:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if (sender != self.nextButton) return;
    if (self.itemSearch.text.length > 0) {

        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSString *result, NSError *error) {
                                        if (!error) {

                                            NSLog(@"Successfully pinged eBay!");
                                        }

                                    }];


    }

    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.

}

在Parse上运行的云端代码(main.js)如下:

Parse.Cloud.define("eBayCategorySearch", function(request, response) {
          url = 'http://svcs.ebay.com/services/search/FindingService/v1';

  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',
       'itemFilter(0).name=ListingType' : 'itemFilter(0).value=FixedPrice',
       'keywords' : request.params.item,

        // your other params
     },
      success: function (httpResponse) {

          response.success(httpResponse.data)

          // count number of times each unique primaryCategory shows up (based on categoryId), return top two (done with a for loop?)

          var userCategories = {};

          data.findItemsByKeywordsResponse.searchResult[0].item.forEach(function(item) 
          {
          var id = item.primaryCategory[0].categoryId;
          if (userCategories[id]) userCategories[id]++;
          else userCategories[id] = 1;
          });

          var top2 = Object.keys(userCategories).sort(function(a, b) 
            {return userCategories[b]-userCategories[a]; }).slice(0, 2);
          console.log('Top two categories: ' + top2.join(', '));


      // deal with success and respond to query
  },
            error: function (httpResponse) {
                console.log('error!!!');
                console.error('Request failed with response code ' + httpResponse.status);
            }
       });
});

我相信这可能是因为 JSON 未正确返回,但我不确定如何确定或如何修复它。感谢您的帮助!

最佳答案

第一步是记录 httpResponse 对象的值。更新您的 success 回调以记录该值:

  success: function (httpResponse) {
      console.log('Received successful response.'); // <---
      console.log(httpResponse); // <---
      response.success(httpResponse.data)

      // count number of times each unique primaryCategory shows up (based on categoryId), return top two (done with a for loop?)

      var userCategories = {};

      data.findItemsByKeywordsResponse.searc

您可以在此处阅读有关如何记录数据的信息:https://parse.com/docs/cloud_code_guide#logging

您可以在此处找到有关阅读日志的更多信息:https://parse.com/docs/cloud_code_guide#clt-logs

关于javascript - 在 Xcode 中收到引用错误,指出 undefined object 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22698502/

相关文章:

javascript - 无法在 react 中解构变量

ios - 是否可以在 iOS 上为视频帧使用循环缓冲区?

ios - 用于转发到 Objective-C 中复制的属性的正确属性语义

ios - 如果已为 UIBackgroundModes 注册了位置,iOS 会唤醒已终止的应用程序吗?

html - 如何修复此 CSS 在 iPhone Safari 上的显示?

iphone - AFNetworking 与 iOS 4.3 不兼容

objective-c - 将 Swift Dictionary 转换为 NSMutableDictionary 的这两个选项有什么区别?

jquery 脚本的 javascript 变量

javascript - 将 "boolean bit array"转换为 Typescript 中的数字

javascript - 为什么使用 .reduce 而不是 reduce()?在使用 JavaScript 扁平化数组中