【问题标题】:Parse.com Cloud Code - After SaveParse.com 云代码 - 保存后
【发布时间】:2014-11-23 23:33:27
【问题描述】:

我希望向 Parse.com 添加一个保存后触发器,当某种类型的用户帐户更新时通知我。在这种情况下,如果 Parse.User 中的“user_ispro”列为真,我希望在保存后通过电子邮件发送(此列要么为空,要么为真)。我在下面添加了代码,但每次更新都会收到电子邮件,而不仅仅是我的查询。想法?

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

【问题讨论】:

    标签: javascript parse-platform mandrill parse-cloud-code


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-02
        • 2014-08-31
        • 2015-08-08
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2016-02-08
        • 2016-02-24
        相关资源
        最近更新 更多