2017-04-12 5 views
1

私はParse-Serverとmailgunのクラウドコードを使用して電子メールにサインアップまたは変更するユーザーに電子メール確認コードを送信しています。PFUserの "email"フィールドが変更されているかどうかを知るためにDirtyKeysを使用する方法?

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 <[email protected]>', 
     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); 
    }); 



}); 

。しかし、ユーザーが電子メールを変更した場合にのみ、このメソッドを使用したいと考えています。

答えて

1

hererequest.object.dirtyKeys()には"email"が含まれているかどうかを確認してください。この場合、電子メールのプロパティが変更され、検証を送信することができます。

このチェックでは、ユーザーの最初の保存(つまり作成)もキャッチされます。メールの送信を避けたい場合は、request.object.isNew()を使用して、操作が作成か更新かを調べることができます。

+0

ありがとうございます!私はそれを解決した:) – Kingofmit

関連する問題