beforeSave 导致查询 return 没有
beforeSave causes Queries to return nothing
我有一个云函数,它接收请求并创建对象,并将它们关联到承诺链中。没有 beforeSave 这个函数工作正常,但是 beforeSave 的存在是为了防止在电子邮件 Class.
中重复输入电子邮件地址
请求
{
"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);
});
});
});
});
保存代码前
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 并在电子邮件分配中创建一个指针 Class。
很乐意进一步解释代码的任何部分或整个函数。
save
与 find
一样,是一种异步方法。要等到它们完成,并注意它们是否失败,您需要对循环中创建的所有承诺使用 Promise.when
。请参阅 和类似问题。
您的代码应如下所示:
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);
});
}));
});
});
我有一个云函数,它接收请求并创建对象,并将它们关联到承诺链中。没有 beforeSave 这个函数工作正常,但是 beforeSave 的存在是为了防止在电子邮件 Class.
中重复输入电子邮件地址请求
{
"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);
});
});
});
});
保存代码前
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 并在电子邮件分配中创建一个指针 Class。
很乐意进一步解释代码的任何部分或整个函数。
save
与 find
一样,是一种异步方法。要等到它们完成,并注意它们是否失败,您需要对循环中创建的所有承诺使用 Promise.when
。请参阅
您的代码应如下所示:
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);
});
}));
});
});