我试图在我的javascript函数中设置处理成功和错误的协议,但是当函数成功时它似乎没有合作。我收到以下错误:
Failed with: TypeError: Object #<Object> has no method 'success'
at Object.Parse.Cloud.httpRequest.success (main.js:77:20)
at Object.<anonymous> (<anonymous>:571:19)
这里有完整的功能:
// Sends search query to eBay
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,
},
success: function (httpResponse) {
// parses results
var response = JSON.parse(httpResponse.text);
var items = [];
response.findItemsByKeywordsResponse.forEach(function(itemByKeywordsResponse) {
itemByKeywordsResponse.searchResult.forEach(function(result) {
result.item.forEach(function(item) {
items.push(item);
});
});
});
// count number of times each unique primaryCategory shows up (based on categoryId), return top two
var categoryResults = {};
items.forEach(function(item) {
var id = item.primaryCategory[0].categoryId;
if (categoryResults[id]) categoryResults[id]++;
else categoryResults[id] = 1;
});
var top2 = Object.keys(categoryResults).sort(function(a, b)
{return categoryResults[b]-categoryResults[a]; }).slice(0, 2);
console.log('Top categories: ' + top2.join(', '));
// compare categoryResults to userCategory object
var userCategory = ['9355'];
var AnyItemsOfCategoryResultsInUserCategory = Object.keys(categoryResults).some(function(item) {
return userCategory.indexOf(item) > -1;
});
console.log('Matches found: ' + AnyItemsOfCategoryResultsInUserCategory);
var ItemsOfCategoryResultsInUserCategory = Object.keys(categoryResults).filter(function(item) {
return userCategory.indexOf(item) > -1;
});
console.log('User categories that match search: ' + ItemsOfCategoryResultsInUserCategory);
response.success('Victory!');
},
error: function (httpResponse) {
console.log('error!!!');
response.error('Request failed with response code ' + httpResponse.status);
}
});
});
它引用的第77行就是这一行:
response.success('Victory!');
答案 0 :(得分:6)
该行:
var response = JSON.parse(httpResponse.text);
您将response
设置为json对象,据我所知,没有response.success
函数添加到对象中。
我想你想要的response
是传入
Parse.Cloud.define("eBayCategorySearch", function(request, response) {
因此您需要重命名其中一个响应对象。