【问题标题】:How to use DirtyKeys to know if "email" field in PFUser has been modified?如何使用 DirtyKeys 知道 PFUser 中的“电子邮件”字段是否已被修改?
【发布时间】:2017-09-08 17:32:48
【问题描述】:

我正在使用 Parse-Server 和 mailgun 的 cloudcode 向注册或更改电子邮件的用户发送电子邮件验证码。

BeforeSave 方法:

Parse.Cloud.beforeSave(Parse.User, function(request, response) {


    var verificationCode = Math.floor(Math.random()*999999);

    var text = "Hi ... this is a verification code:" + verificationCode;


    var data = {
      from: 'WBP Team <info@test.eu>',
      to: request.object.get("email"),
      subject: 'Please verify your e-mail for you Account',
      text: text
    };

    mailgun.messages().send(data, function (error, body) {

      if (error) {

            response.error("Email not sent..."+error);

      }else{

            var user = Parse.User.current();
            user.set("emailVerificationCode", verificationCode);
            user.save();

            response.success("Email Sent");
      }

      console.log(body);
    });



});

现在每次用户修改任何文件时都会发送电子邮件。但我只想在用户更改电子邮件时使用该方法。

【问题讨论】:

    标签: parse-server parse-cloud-code mailgun email-verification


    【解决方案1】:

    这已经回答here:你只需检查request.object.dirtyKeys()是否包含"email";如果是这种情况,则电子邮件属性已更改,您可以发送验证。

    请注意,此检查还会捕获用户的第一次保存(即创建);如果您想避免发送邮件,您可以使用request.object.isNew() 来确定操作是创建还是更新。

    【讨论】:

      猜你喜欢
      • 2014-12-26
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多