我想动态添加一个键到一个对象,例如,我运行一个查询来获取Items(类Test),然后为每个项目计算它所拥有的Favorite Records的数量(假设1是UserId)。但是,当我从iOS(Swift)调用函数时,我得到PFObject列表,但没有动态密钥(我不想保存Key on Items(类Test),因为那只是为了即时获取数据。
Parse.Cloud.define("getAllItemsTestInstag",function(request,response){
var query = new Parse.Query("Test");
query.find().then(function(tests){
var elementProcessed = 0;
for (var i = 0; i < tests.length; i++) {
var testItem = tests[i];
var innerQuery = new Parse.Query("Favorite");
innerQuery.equalTo("UserId",1);
innerQuery.equalTo("Test",testItem);
innerQuery.count({
success: function(count){
if(count == 0){
testItem.set("related",true);
}else{
testItem.set("related",false)
}
console.log("Relacion conteo : " + count);
elementProcessed++;
if(elementProcessed == tests.length){
response.success(tests);
}
},
error: function(error){
console.log(error);
}
});
};
});
});
答案 0 :(得分:0)
您只需添加新密钥而不保存,然后在response.success中将其返回。像这样:
...
innerQuery.count({
success: function(count){
if(count == 0){
testItem.set("related",true);
}else{
testItem.set("related",false)
}
testItem.count = count;
console.log("Relacion conteo : " + count);
elementProcessed++;
if(elementProcessed == tests.length){
response.success(tests);
}
},
error: function(error){
console.log(error);
}
});
...