我有一个AngularJS应用程序,人们可以在这里评论文章,比如在博客中。该表单工作得很好,但我想在保存到解析之前验证cloud code
中的输入。
我的问题是,无论我做什么,似乎输入都会被保存——即使是未定义的值。
Parse.Cloud.define("saveComment", function(request, response) {
"use strict";
var Comment = Parse.Object.extend("Comment");
var comment = new Comment();
comment.set("commentName", request.params.commentName);
comment.set("commentEmail", request.params.commentEmail);
comment.set("commentWebsite", request.params.commentWebsite);
comment.set("commentContent", request.params.commentContent);
comment.set("commentFor", {__type:"Pointer", className:"Article", objectId:request.params.commentFor});
comment.set("commentApproved", false); // need to add a check for Commenter status
/* ADD PERMISSIONS */
var acl = new Parse.ACL();
acl.setPublicReadAccess(true);
acl.setPublicWriteAccess(false);
comment.setACL(acl);
Parse.Cloud.useMasterKey();
comment.save().then(function(comment) {
response.success(comment);
}, function(error) {
// error handling
response.error(error);
});
});
Parse.Cloud.beforeSave("saveComment", function(request, response) {
"use strict";
var regexEmail = /^([w-]+(?:.[w-]+)*)@((?:[w-]+.)*w[w-]{0,66}).([a-z]{2,6}(?:.[a-z]{2-10})?)$/i;
if (request.object.get("commentTest").length === 0) {
response.error(100);
}
else if (request.object.get("commentName").length === 0) {
response.error(200);
}
else if (!regexEmail.test(request.object.get("commentEmail"))) {
response.error(300);
}
else {
response.success();
}
});
在上面的示例中,我还尝试了var === undefined
和其他一些简单输入验证变体,以及您可以看到的regex。
问题是,无论我在表单中的输入字段中键入什么,它都会被保存——就好像beforeSave函数从未被触发一样。
如有任何帮助,我们将不胜感激。
自己发现了错误。。。
事实证明,beforeSave()
需要对象作为第一个参数,而不是它所连接的函数:
Parse.Cloud.beforeSave("Comment", function(request, response) {
// stuff
});
如下所述:https://parse.com/docs/cloudcode/guide#cloud-代码前保存触发