我目前有我的云代码设置,在解析eBay的数据库时返回前两个类别ID。
我还在Parse的后端设置了用户系统,以便每个User
都有与其帐户关联的userCategory
个对象的多个实例。每个userCategory
实例都具有唯一的categoryId
属性。
如何让我的eBayCategorySearch
函数迭代所有用户的userCategory
个实例,看看是否有任何categoryId
属性与ebay返回的top2
匹配?我试图在下面这样做,但据我所知,这只有在userCategory
是一个数组而不是一个对象的多个实例时才有效。
用户注册时初始化userCategory
对象的函数:
Parse.Cloud.define("userCategoryCreate", function(request, response) {
var userCategory = Parse.Object.extend("userCategory");
var newUserCategory = new userCategory();
newUserCategory.set("categoryId", "9355");
newUserCategory.set("minPrice");
newUserCategory.set("maxPrice");
newUserCategory.set("itemCondition");
newUserCategory.set("itemLocation");
newUserCategory.set("parent", Parse.User.current());
newUserCategory.save({
success: function (){
console.log ('userCategory succesfully created!');
response.success('Request successful');
},
error: function (){
console.log('error!!!');
response.error('Request failed');
}
});
});
eBayCategorySearch函数:
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 httpresponse = JSON.parse(httpResponse.text);
var items = [];
httpresponse.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 = Parse.User.userCategory;
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(AnyItemsOfCategoryResultsInUserCategory);
},
error: function (httpResponse) {
console.log('error!!!');
response.error('Request failed with response code ' + httpResponse.status);
}
});
});
答案 0 :(得分:0)
这样的东西?
// Ebay ids (Hopefully you can get to this point. If it's a collection, _.map() etc.)
var ebayIDs = ['x1','x2'];
// UserCategoryCollection.models (assuming you're working with a collection .models is where the array of Parse Objects are... here I'm just simply declaring the example)
var userCategoryCollection.models = [{id:'u1',attributes:{categoryId:'a1'}},{id:'u2',attributes:{categoryId:'x1'}},{id:'u3',attributes:{categoryId:'x2'}}]
// How to figure out if there is a match... use Underscore
// Use the find function to iterate through the models, each model being the arg passed
var found = _.find(userCategoryCollection.models, function(userCategory){
// to the evaluation which will return true if the Parse object prop === a value in the ebayIDs array
return _.contains(ebayIDs, userCategory.get('categoryId'));
});
// Evaluate
if (found) {
console.log('hurrah!');
} else {
console.log('nope');
}
从您的问题中抽象出来,但如果我理解您的问题,概念/方法应该适用。