我写了一系列Parse Promises,当我向这个云代码函数发出请求时,我现在收到错误141。我试过放置成功:/ error:基于Parse DOCS,我认为它们所属的功能。
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": [
"max@gmail.com",
"nat@gmail.com",
"noob@gmail.com"
],
"userId": "sLmOf4fZFL"
}
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;
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(results) {
projectO = results;
console.log(projectO);
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) {
var emailJSON = results[0].toJSON();
var emailObjectId = emailJSON.objectId;
var projectJSON = projectO.toJSON();
var projectId = projectJSON.objectId;
var assignment = new EmailsClassAssignment();
assignment.set("createdBy", {
"__type": "Pointer",
"className": "_User",
"objectId": user
});
assignment.set("email", {
"__type": "Pointer",
"className": "Email",
"objectId": emailObjectId
});
assignment.set("project", {
"__type": "Pointer",
"className": "Project",
"objectId": projectId
});
assignment.save(null, {
success: function() {
console.log("Successfully saved project");
},
error: function(error) {
console.log("There was an error saving" + error.message);
}
});
});
}));
}).then( function() {
response.success();
});
});
答案 0 :(得分:4)
基本的想法看起来还不错,但代码是一堆混乱的回调参数和承诺。我冒昧地将重构变成更简单,更有承诺的逻辑块,以便我们可以看到发生了什么。
您在帖子中突出显示了.map功能。不知道那里有什么问题,所以我建议的代码使用了underscorejs,它可以很容易地包含在云中,如下所示:
var _ = require('underscore');
首先,返回一个保证“项目”的承诺,将大部分参数保存到您的云函数中:
function createProject(params) {
var ProjectClass = Parse.Object.extend("Project");
var project = new ProjectClass();
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;
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);
return project.save();
}
接下来,在给定一组电子邮件地址字符串的情况下创建“电子邮件”(它们是对象)。 (你可以更好地区分命名中的对象和字符串,但我试着在代码中找到原始的命名法)
function createEmails(emails) {
var EmailsClass = Parse.Object.extend("Email");
var toSave = _.map(emails, function(emailAddress) {
var email = new EmailsClass();
email.set("address", emailAddress);
return email;
});
// like the when() function, but (possibly) fewer requests
return Parse.Object.saveAll(toSave);
}
这是原始代码转向恶化的地方。在其中,代码刚刚完成创建Email对象,然后由于某种原因,它尝试查询这些对象。但是,我们已经将它们掌握在手中,充分履行了拯救的承诺。
以下方法采用已构建的电子邮件对象(以迂腐的方式命名,强调它们是对象)以及“EmailClassAssignment”的其他成分。注意当我们手头有PFObject时,我们如何直接用对象指定指针:
function createEmailClassAssignments(emailObjects, project, userId) {
var EmailsClassAssignment = Parse.Object.extend("EmailAssignment");
var toSave = _.map(emailObjects, function(emailObject) {
var assignment = new EmailsClassAssignment();
// the real objects can be used as parameters to set for pointer columns
assignment.set("email", emailObject);
assignment.set("project", project);
// we only have the userId, not a user object, so we can either query
// for the user or take the shortcut that you've been taking
project.set("createdBy", {
"__type": "Pointer",
"className": "_User",
"objectId": user
});
return assignment;
});
return Parse.Object.saveAll(toSave);
}
完成所有这些后,云功能变得更加清晰:
Parse.Cloud.define("saveProject", function(request, response) {
var project;
createProject(params).then(function(result) {
project = result;
return createEmails(request.params.shareEmails);
}).then(function(emailObjects) {
return createEmailClassAssignments(emailObjects, project, request.params.userId);
}).then(function() {
console.log("Successfully saved project");
// I took the liberty of returning the new project to the caller
response.success(project);
}, function(error) {
console.log("There was an error saving" + error.message);
resoonse.error(error);
});
});
注意:显然,我无法测试任何前述内容。我强烈建议您自己测试这些功能,最好是在期望组合工作之前单独测试。希望重构器能够更清晰地使用promises和合理的部件分解来单独测试和使用。
答案 1 :(得分:2)
从代码的外观来看,您只需在return
前面添加assignment.save()
,因为您还没有等待其他方式完成。
最后,您应该在最后添加错误捕获器:
.then(null, function(error) {
console.log(error);
response.error(error);
});