I currently have my cloud code setup to return the top two category Ids when parsing eBay's database.
I also have the user system setup on Parse's backend so that each User
has multiple instances of the userCategory
object associated with their account. Each userCategory
instance has a unique categoryId
property.
How can I have my eBayCategorySearch
function iterate through all a User's userCategory
instances, and see if any have a categoryId
property that matches the top2
being returned by ebay? I've attempted to do so below, but as far as I can understand, this will only work if userCategory
is an array, not multiple instances of an object.
Function that initializes userCategory
object when a user signs up:
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 function:
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);
}
});
});
Something like this?
// 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');
}
It's a little abstracted from your question but the concept/method should be applicable if I understand your question correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.