2014-03-27 30 views
0

我正在寫解析雲代碼,它會ping通易趣,返回帶有項目結果的JSON數據,然後解析該數據並將前兩個類別存儲到數組中。發送給eBay的查詢基於用戶在iOS應用程序的itemSearch欄中輸入的內容。當我試圖發送一個查詢類似「iPhone」,它給了我一個錯誤,說明如下:在Xcode中接收到引用錯誤,指出沒有定義對象。

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. 

} 

雲代碼(主。在解析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,但我不知道我怎麼會確保,或如何可以固定。任何幫助表示讚賞!

+0

谷歌JSON驗證你會看到幾個網站可用來驗證你的JSON。 – Jeremy

回答

1

第一步是記錄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

+0

添加上面導致它記錄'收到成功的響應。',但是我仍然得到我上面提到的錯誤。 – KoftaClarence

+0

之後有什麼記錄? httpResponse是否包含一個值? –

+0

這是在我的日誌中發現的: '輸入:{「item」:「iphone」} 失敗:ReferenceError:數據未定義 在Object.Parse.Cloud.httpRequest.success(main.js:35 :11) at Object。 :565:19) I2014-03-28T04:41:48.853Z] Received received response.' – KoftaClarence

相關問題