我是Parse的新手,我只是创建了我的第一个Cloud Code,我遇到了Parse.Query
没有完成的问题,因为显然response.success()
显然会被过早调用。< / p>
Parse.Cloud.beforeSave("items",function(request,response){
hasPictures = calc(request);
request.object.set("hasPictures",hasPictures);
response.success();
});
function calc(request){
var hasPictures = false;
var userPictureCount = getPictureCountForUser(request.object.get("uniqueIdentifier"));
// something else is being calculated here. removed it so simplify
if(userPictureCount > 0)
hasPictures = true;
return hasPictures;
}
var getPictureCountForUser = function(uniqueIdentifier){
var query = new Parse.Query("items");
if(Parse.User.current()){
a.equalTo("userId", Parse.User.current().id);
}else{
query.equalTo("uniqueIdentifier", uniqueIdentifier);
}
query.find({
success: function(results){
//console.log(results.length + ' pictures found!');
console.log(results);
// return results.length;
response.success(results);
},
error: function(error) {
response.error("Query failed. Error = " + error.message);
}
});
};
我知道我不得不处理Promises。但我不知道如何实现它。
Parse.Query
getPictureCountForUser()
中的response.success()
从未发现过某些内容,因为calc()
位于错误的位置。我必须在哪里放置它,特别是在response.success()
之间?
这样每个函数都会等到另一个函数完成,然后再过早发送=and(not(len(A1)=17),not(isblank(a1)))
。
谢谢!
答案 0 :(得分:0)
你可以使用promises但是对于你的特定情况,如果你不想使用promises,你只能使用回调,不再是这样的:
Parse.Cloud.beforeSave("items",function(request,response){
hasPictures = calc(request, function(hasPictures) {
// i don't know your logic, you could do a response.success of failure, what do you need
request.object.set("hasPictures",hasPictures);
response.success();
});
});
function calc(request, callback){
var hasPictures = false;
getPictureCountForUser(request.object.get("uniqueIdentifier"), function(userPictureCount) {
if(userPictureCount > 0)
hasPictures = true;
callback(hasPictures);
});
};
var getPictureCountForUser = function(uniqueIdentifier, callback){
var query = new Parse.Query("items");
if(Parse.User.current()){
a.equalTo("userId", Parse.User.current().id);
}else{
query.equalTo("uniqueIdentifier", uniqueIdentifier);
}
query.find({
success: function(results){
callback(results.length);
},
error: function(error) {
callback(-1);
//response.error("Query failed. Error = " + error.message);
}
});
};