2

I am having trouble with my aftersave handler. So when a new user logs in, it checks to see of both tProfile and sProfile fields are empty, if so it saves saves an acl for the user and then sends an email to the user using nodemailer nmp package. But after a user is saved the server keeps on sending email after email in a loop, there were about 64 emails sent after which google blocked out the login attemps, help would be very appreciated. My cloud code is given below

Parse.Cloud.afterSave(Parse.User, (request) => {
        const user = request.object;
        const t = user.get('tProfile');
        const s = user.get('sProfile');
        if (!t && !s) {
        user.setACL(new Parse.ACL(user));
        user.save(null,{ useMasterKey: true });
        sendWelcomeEmail(user.getUsername(),user.get('type'));
        return;
        }else{
            console.log("Condition Working");
            return;
        }
    });

const sendWelcomeEmail = (userName,type) => {
        var nodemailer = require('nodemailer');
        var transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
            user: 'tanzim3421@gmail.com',
            pass: 'oyhlyk****ocvhaa'
          }
        });
        var mailOptions = {
          from: 'tutorbeargroup@gmail.com',
          to: userName,
          subject: 'Welcome to TutorBear',
          text: 'Dear '+type+', thankyou for signing up with us. If you have any questions please contact us at: 01726409161'
        };
        transporter.sendMail(mailOptions,(error, info)=>{
          if (error) {
            console.log(error);
          } else {
           console.log('Email sent: ' + info.response);
          }
        });
    }
Tanzim Chowdhury
  • 3,020
  • 2
  • 9
  • 21
  • It is obvious it will be a recursive function If you try to save in afterSave. If you want to modify user data you should use beforeSave. – Suat Karabacak Feb 21 '20 at 09:34
  • So from what you are saying , any aftersave handler I define will be recursive ?? My aim is to send an email to new users after they sign up , how can I do that without aftersave ? I dont want to call another cloudcode function after signup from client . I want to send an email as soon as they are signed up and thats only possible through cloud code, I cannot use before save because there is not before signup handler – Tanzim Chowdhury Feb 21 '20 at 09:43
  • And I dont get why aftersave is recursive, shouldnt after save be called once per save of the user class ? or does it just keep on runnin ? – Tanzim Chowdhury Feb 21 '20 at 09:47
  • It is recursive because your are defining a trigger for save event for User class so if you call save function in trigger it will be an endless loop. With afterSave you can make queries for same class or save actions for different classes. You can send e-mail to new users if you check your user if exist in beforeSave. – Suat Karabacak Feb 21 '20 at 09:59
  • Oh my, thankyou so much, I feel so dumb right now. – Tanzim Chowdhury Feb 21 '20 at 11:12

1 Answers1

1

If you try to save same class object in afterSave it will be recursive function.

You can use beforeSave trigger

Parse.Cloud.beforeSave(Parse.User,async(req)=>{
  let userObject = req.object;
  if(userObject.isNew()){
    //Your Logic
  }
});
Suat Karabacak
  • 643
  • 2
  • 7
  • 24