0

I was hoping to add an after save trigger to Parse.com that notified me when a certain type of user's account was updated. In this case, if column "user_ispro" is true in Parse.User I want to be emailed after the save (this column is either null to true). I added the code below but I am getting emailed on every update instead of just my query. Thoughts?

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");
        }
    });
});
4

2 回答 2

1

查询的成功回调总是被执行(阅读:当查询成功时),几乎在所有情况下都是如此。当没有结果时,您期望查询失败,这是错误的假设。

您应该添加一个检查结果是否为空,并且只有在有实际结果时才触发电子邮件发送。错误回调仅在发生错误时触发,空结果不是错误(显然)。

于 2014-09-29T14:40:16.580 回答
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");
    }
});
});
于 2014-09-30T18:51:51.017 回答