mardi 2 octobre 2018

If statement running after function being called

I'm setting up a mailer to send an email to the relevant recipient about some details of an appointment request.

I'm having an issue where the IF statement that runs to decide who the necessary recipient should be is running after the email transporter and is giving me an error saying that no recipient has been defined.

Here is the code

let recipientEmail;

if (careHome === 'ACareHome') {
    admin.database().ref('managers').once("value").then((snapshot) => {
        let managerEmail = snapshot.child("Manager Name").val();
        recipientEmail = managerEmail;
        console.log(`Recipient is ${recipientEmail}`);
   });
}

const mailOptions = {
    from: '*****', // sender address
    subject: `An appointment has been requested at ${ACareHome}.`,
    html: `Hello, an appointment has been booked at ${ACareHome} on ${date} at ${time}. The requestors name is, ${firstName}. You can email them on ${email}.` 
};

mailOptions.to = recipientEmail;

transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
})

In the Firebase logs, I am getting these errors

enter image description here

It's showing that the IF statement is being run after the transporter function causing it to error.

I've tried all manor of things but cant seem to get it to play ball!

Help is appreciated, cheers!

Aucun commentaire:

Enregistrer un commentaire