In the process of developing parse cloud code to interact with eBay, fetch JSON data containing item results, and extract the top two categories for storage in an array, I encountered an issue. The query sent to eBay is determined by user input in the itemSearch bar within my iOS application. However, when attempting a query like "iPhone", an error message is triggered:
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)
The associated objective-c code reads as follows:
- (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.
}
The Parse cloud code (main.js) responsible for executing on Parse servers is outlined below:
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(', '));
// explanation of successful execution and response handling
},
error: function (httpResponse) {
console.log('error!!!');
console.error('Request failed with response code ' + httpResponse.status);
}
});
});
I suspect the issue may be related to the incorrect handling of returned JSON data, but I am uncertain about how to verify this or rectify it. Any guidance or assistance would be greatly appreciated!