Parse.com 保存前不起作用
Parse.com beforesave wont work
我有一个 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
和简单输入验证的许多其他变体,以及您所看到的正则表达式。
问题是,无论我在表单的输入字段中键入什么,它都会被保存 - 就好像从未触发过 beforeSave 函数一样。
如有任何帮助,我们将不胜感激。
自己发现错误...
事实证明,beforeSave()
需要对象作为第一个参数,而不是它所连接的函数:
Parse.Cloud.beforeSave("Comment", function(request, response) {
// stuff
});
如此处解释:https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers
我有一个 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
和简单输入验证的许多其他变体,以及您所看到的正则表达式。
问题是,无论我在表单的输入字段中键入什么,它都会被保存 - 就好像从未触发过 beforeSave 函数一样。
如有任何帮助,我们将不胜感激。
自己发现错误...
事实证明,beforeSave()
需要对象作为第一个参数,而不是它所连接的函数:
Parse.Cloud.beforeSave("Comment", function(request, response) {
// stuff
});
如此处解释:https://parse.com/docs/cloudcode/guide#cloud-code-beforesave-triggers