我希望在Parse.com上添加一个保存后触发器,当某个类型的用户帐户被更新时通知我。在这种情况下,如果Parse.User中的列“user_ispro”为true,我希望在保存后通过电子邮件发送(此列为null为true)。我添加了下面的代码,但我收到的是每次更新的电子邮件,而不仅仅是我的查询。想法?
Parse.Cloud.afterSave(Parse.User, function(request) {
var Mandrill = require('mandrill');
query = new Parse.Query(Parse.User);
query.equalTo("user_ispro", true);
query.find({
success: function(results) {
Mandrill.initialize('xxxx');
Mandrill.sendEmail({
message: {
text: "Email Text",
subject: "Subject",
from_email: "test@test.com",
from_name: "Test",
to: [{
email: "test@test.com",
name: "Test"
}]
},
async: true
}, {
success: function(httpResponse) {
console.log(httpResponse);
response.success("Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});
},
error: function() {
response.error("User is not Pro");
}
});
});
答案 0 :(得分:1)
总是执行查询的成功回调(读取:查询成功时),几乎在所有情况下都是如此。当没有结果时,您期望查询失败,这是一个错误的假设。
如果结果为空,您应该添加一个检查,只有在有实际结果时才会触发电子邮件发送。如果出现错误,则仅触发错误回调,空结果不是错误(显然)。
答案 1 :(得分:0)
谢谢Bjorn,我最终使用了计数查询而不是查找。如果结果数大于0,则发送电子邮件。还意识到我没有查询特定的objectId所以这是我的最终代码:
Parse.Cloud.afterSave(Parse.User, function(request) {
var Mandrill = require('mandrill');
query = new Parse.Query(Parse.User);
query.equalto("objectId",request.object.id);
query.equalTo("user_ispro", true);
query.count({
success: function(count) {
if (count > 0) {
Mandrill.initialize('xxxx');
Mandrill.sendEmail({
message: {
text: "Email Text",
subject: "Subject",
from_email: "test@test.com",
from_name: "Test",
to: [{
email: "test@test.com",
name: "Test"
}]
},
async: true
}, {
success: function(httpResponse) {
console.log(httpResponse);
response.success("Email sent!");
},
error: function(httpResponse) {
console.error(httpResponse);
response.error("Uh oh, something went wrong");
}
});
},
else {
console.log("User is not Pro");
}
});
});