我有一个云函数,它接受请求并创建对象,并将它们与一系列承诺相关联。如果没有beforeSave,此函数可以正常工作,但beforeSave存在是为了防止电子邮件类中的电子邮件地址重复输入。
REQUEST
{
"projectDescription": "Testing saveProject",
"projectTitle": "This is only a test, in the event of a real post this will have an actual description",
"isEmailEnabled": true,
"shareEmails": [
"test1@gmail.com",
"test2@gmail.com",
"test3@gmail.com"
],
"userId": "1234"
}
功能
Parse.Cloud.define("saveProject", function(request, response) {
var emails = request.params.shareEmails;
var user = request.params.userId;
var projectDescription = request.params.projectDescription;
var projectTitle = request.params.projectTitle;
var emailStatus = request.params.isEmailEnabled;
var ProjectClass = Parse.Object.extend("Project");
var EmailsClass = Parse.Object.extend("Email");
var EmailsClassAssignment = Parse.Object.extend("EmailAssignment");
var project = new ProjectClass();
var projectO;
var emailQueryArray;
project.set("title", projectTitle);
project.set("createdBy", {
"__type": "Pointer",
"className": "_User",
"objectId": user
});
project.set("description", projectDescription);
project.set("status", true);
project.set("emailShareEnabled", emailStatus);
project.save().then(function(projectObject) {
projectO = projectObject;
}).then(function() {
emails.forEach(function(emailAddress) {
var email = new EmailsClass();
email.set("address", emailAddress);
email.save();
});
}).then(function() {
emails.forEach(function(emailQuery) {
var queryEmail = new Parse.Query("Email");
queryEmail.equalTo("address", emailQuery);
queryEmail.find().then(function(results) {
emailObject = results;
console.log(emailObject);
});
});
});
});
beforeSave Code
Parse.Cloud.beforeSave("Email", function(request, response) {
var query = new Parse.Query("Email");
// Gets the email key value (string) before object is saved
query.equalTo("address", request.object.get("address"));
// Checks to see if an object for that email already exists
query.first({
success: function(object) {
if (object) {
response.error("This email already exisits");
} else {
response.success();
}
},
error: function(error) {
response.error("Could not determine if this email exists");
}
});
});
有没有办法在保存电子邮件对象时考虑回调中的beforeSave,以便我确保对象可以查询?
我需要将对象放在那里,以便我可以为每个对象获取Id,并在Email assignments Class中创建一个指针。
很乐意进一步解释代码或功能的任何部分作为一个整体。
答案 0 :(得分:1)
save
就像find
一样,是一种异步方法。要等到它们完成,并注意它们是否已经失败,您需要对循环中创建的所有承诺使用Promise.when
。请参阅Parse JavaScript Promises within Loop not Completing和类似问题。
您的代码应如下所示:
var ProjectClass = Parse.Object.extend("Project");
var EmailsClass = Parse.Object.extend("Email");
var EmailsClassAssignment = Parse.Object.extend("EmailAssignment");
Parse.Cloud.define("saveProject", function(request, response) {
var emails = request.params.shareEmails;
var user = request.params.userId;
var projectDescription = request.params.projectDescription;
var projectTitle = request.params.projectTitle;
var emailStatus = request.params.isEmailEnabled;
var project = new ProjectClass();
project.set("title", projectTitle);
project.set("createdBy", {
"__type": "Pointer",
"className": "_User",
"objectId": user
});
project.set("description", projectDescription);
project.set("status", true);
project.set("emailShareEnabled", emailStatus);
project.save().then(function() {
return Parse.Promise.when(emails.map(function(emailAddress) {
// ^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^
var email = new EmailsClass();
email.set("address", emailAddress);
return email.save();
// ^^^^^^
}));
}).then(function() {
return Parse.Promise.when(emails.map(function(emailQuery) {
// ^^^^^^ ^^^^^^^^^^^^^^^^^^ ^^^
var queryEmail = new Parse.Query("Email");
queryEmail.equalTo("address", emailQuery);
return queryEmail.find().then(function(results) {
// ^^^^^^
console.log(results);
});
}));
});
});