0

The following is intended to make various calls to a db to pull users who must be emailed and email each of those users with a personalized message. The emailService.sendEmail calls an smtp nodemailer service which is working correctly. However the function is executing multiple times in parallel, resulting in emails being sent three times to each recipient. For example, 'const response = await SQL.getProjectsWithCurrentUTCDate();' is returning two different responses, each of which are iterated through the entire function.

var job = schedule.scheduleJob('*/15 * * * *', async function () {

        try {

            // first, get projects that have a reminder date or start date of current day
            const response = await SQL.getProjectsWithCurrentUTCDate();


            // next, for each project
            response.rows.forEach(async function (project) {
                // Create a projectData object
                var projectData = new Object();
                projectData.projectName = project.name;
                projectData.startDate = project.start_date.toISOString().substring(0, 10);
                try { projectData.reminder1Date = project.first_reminder_date.toISOString().substring(0, 10); } catch (ignore) { }
                try { projectData.reminder2Date = project.second_reminder_date.toISOString().substring(0, 10); } catch (ignore) { }
                projectData.endDate = project.end_date.toISOString().substring(0, 10);
                projectData.fields = project.fields;
                // get revalidator emails who have incomplete revalidations for the given project
                const secondResponse = await SQL.getRevalidatorsForIncompleteRevalidationsForProjectId(project.id);
                // finally, send email to those revalidators
                const emailMessage = EmailService.emailMessageFromProjectData(projectData);
                secondResponse.rows.forEach(function (element) {

                    //var sent = false;
                    //console.log("sent= " + sent);


                    const data = {
                        from: project.email,
                        to: [element['revalidator_email']],
                        subject: project.subject,
                        message: emailMessage.replace(/@@EMAIL@@/g, element['revalidator_email'])
                    };

                    EmailService.sendEmail(data); 


                });
            });

        } catch (err) {
            console.log('Email scheduler failed with error: ' + err);
        }
    });
  • If you add `console.log('test`) after first line `async function () {` will you see several messages during 15 minutes? – Anatoly Nov 18 '20 at 20:37
  • Have you confirmed that your sql queries are returning as expected. You have a nested loop. Your outer `response.rows.forEach` and your inner `secondResponse.rows.forEach`. Is it possible that the same addresses are being returned and because you are have nested forEach's you could be mailing twice? – jeeves Nov 18 '20 at 20:42

0 Answers0