为什么我的代码片段在检索到结果后似乎没有进入 query.find() 函数的分支?有什么想法吗?
Parse.Cloud.beforeSave("AccountTest", function(request, response) {
var email = request.object.get("email");
var accountTest = new Parse.Object.extend("AccountTest");
var query = new Parse.Query(accountTest);
console.log(email);
query.equalTo("email",email);
query.find({
success: function(results) {
console.log("checking");
if (results.length == 0) {
console.log("SUCCESS");
response.success();
}else{
console.log("FAIL");
response.error("E-mail has already been used");
}
console.log('no branch taken');
},
error: function() {
response.error("Something went wrong with the query");
}
});
console.log("OUT OF SCOPE")
response.error("Something went wrong");
});
parse.com 上的日志记录了电子邮件和“超出范围”和“出了点问题”,但它永远不会进入成功或错误分支,有什么想法吗?我不知道 query.find() 函数出了什么问题。
最佳答案
因为 find
函数的异步特性!在 find
函数完成更改之前,您的 beforeSave
代码到达最后一行 response.error
。当您调用 find
函数时,由于其异步特性,它会立即返回。它的回调只会在它完成时被调用。如果您只是删除最后一行 response.error("Something went wrong");
您的函数应该按预期工作。
关于javascript - 解析云代码 : beforeSave query return code not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33869577/