我想要做的是在我的 Parse 云代码中运行一个查询,比较 top2
的两个内容数组到categoryId
每个的属性(property)userCategory
与用户关联的对象。然而,当我运行此代码时,尽管数据库中有匹配项,但它无法正确找到匹配项。
我认为问题出在这里:
query.containedIn('categoryId', top2);
query.equalTo('User', Parse.User.current())
我认为也许categoryId
不是正确的使用方式。我也不完全确定第二行,因为它似乎会比较 top2
到 User 对象,而不是与其关联的 userCategory 对象的实例。
完整代码:
// Query sent from search bar
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), returns top two IDs and their respective names
var categoryIdResults = {};
// Collect two most frequent categoryIds
items.forEach(function(item) {
var id = item.primaryCategory[0].categoryId;
if (categoryIdResults[id]) categoryIdResults[id]++;
else categoryIdResults[id] = 1;
});
var top2 = Object.keys(categoryIdResults).sort(function(a, b)
{return categoryIdResults[b]-categoryIdResults[a]; }).slice(0, 2);
console.log('Top category Ids: ' + top2.join(', '));
var categoryNameResults = {};
// Collect two most frequent categoryNames
items.forEach(function(item) {
var categoryName = item.primaryCategory[0].categoryName;
if (categoryNameResults[categoryName]) categoryNameResults[categoryName]++;
else categoryNameResults[categoryName] = 1;
});
var top2Names = Object.keys(categoryNameResults).sort(function(a, b)
{return categoryNameResults[b]-categoryNameResults[a]; }).slice(0, 2);
console.log('Top category Names: ' + top2Names.join(', '));
// compare categoryIdResults to userCategory object
//Extend the Parse.Object class to make the userCategory class
var userCategory = Parse.Object.extend("userCategory");
//Use Parse.Query to generate a new query, specifically querying the userCategory object.
query = new Parse.Query(userCategory);
//Set constraints on the query.
query.containedIn('categoryId', top2);
query.equalTo("User", Parse.User.current())
//Submit the query and pass in callback functions.
var isMatching = false;
query.find({
success: function(results) {
var userCategoriesMatchingTop2 = results;
console.log("userCategory comparison success!");
console.log(results);
if (userCategoriesMatchingTop2 && userCategoriesMatchingTop2.length > 0) {
isMatching = true;
}
response.success({
"results": [
{ "Number of top categories": top2.length },
{ "Top category Ids": top2 },
{ "Top category names": top2Names },
{ "Number of matches": userCategoriesMatchingTop2.length },
{ "User categories that match search": userCategoriesMatchingTop2 }
]
});
console.log('User categories that match search: ' + results)
},
error: function(error) {
//Error Callback
console.log("An error has occurred");
console.log(error);
}
});
},
error: function (httpResponse) {
console.log('error!!!');
response.error('Request failed with response code ' + httpResponse.status);
}
});
});
用户类别结构:
Parse.Cloud.define("userCategoryCreate", function(request, response) {
var userCategory = Parse.Object.extend("userCategory");
var newUserCategory = new userCategory();
newUserCategory.set("categoryId", "");
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 successfully created!');
response.success('Request successful');
},
error: function (){
console.log('error!!!');
response.error('Request failed');
}
});
});
日志:
I2014-05-13T18:11:25.243Z] v169: Ran cloud function eBayCategorySearch for user jKs5QpXjpd with:
Input: {"item":"iphone 5 16gb"}
Result: {"results":[{"Number of top categories":1},{"Top category Ids":["9355"]},{"Top category names":["Cell Phones & Smartphones","Mobile & Smart Phones"]},{"Number of matches":0},{"User categories that match search":[]}]}
I2014-05-13T18:11:26.530Z] Top category Ids: 9355
I2014-05-13T18:11:26.531Z] Top category Names: Cell Phones & Smartphones, Mobile & Smart Phones
I2014-05-13T18:11:26.633Z] userCategory comparison success!
I2014-05-13T18:11:26.633Z] []
I2014-05-13T18:11:26.633Z] User categories that match search:
最佳答案
您似乎正在尝试匹配不存在的字段。基于创建代码,
query.equalTo('User', Parse.User.current())
应该是
query.equalTo('parent', Parse.User.current())
关于javascript - 解析查询未正确比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23636865/