I'm working with an aggregation to find matches so whenever two people select the same location, time and date it generates a new collection that is pushed ($out) to a matches document. I've generated 2 matches for testing and the structure of the document looks like this.
console.log(email)
[
{ Data: [ [Object], [Object] ], Total: 2 },
{ Data: [ [Object], [Object] ], Total: 2 }
]
Now my intention is to send a email to each person in each match with the data of the other person, I used Nodemailer in the next way.
router.get('/email', async (req, res) => {
const email = await Match.find()
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user:'xxxxx@gmail.com',
pass: 'xxxx'
}
});
let mail_option = {
from: 'Test',
to: email[0]._doc.Data[0].Email,
subject: 'This is a test',
html: ` ${email[0]._doc.Data[1].Name} ${email[0]._doc.Data[1].Surname}. <br/><br/>
${email[0]._doc.Data[1].Email} `
};
let mail_option1 = {
from: 'Test',
to: email[0]._doc.Data[1].Email,
subject: 'This is a test',
html: `${email[0]._doc.Data[0].Nombre} ${email[0]._doc.Data[0].Apellido}. <br/><br/>
${email[0]._doc.Data[0].Email}`
};
transporter.sendMail(mail_option, (err, info) =>{
if(err){
console.log(err);
}else{
console.log('Works')
}
});
transporter.sendMail(mail_option1, (err, info) =>{
if(err){
console.log(err);
}else{
console.log('Also works')
}
});
res.json('Sent');
});
I got it working sending emails nexr I'm trying to get it to send the emails to everyone in the matchs collections. I'm trying with the next example but it doesnt send the email, looks like it doesnt even enter the for as it doesnt give any console log and go straight to the res.json:
router.get("/email", async (req, res) => {
const email = await Match.find();
let data = email[0];
for (data; data < email.length; data++) {
let transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: "xxxxx@gmail.com",
pass: "xxxx",
},
});
let mail_option = {
from: "Test",
to: data._doc.Data[0].Email,
subject: "This is a test",
html: ` ${data._doc.Data[1].Name} ${data._doc.Data[1].Surname}. <br/><br/>
${data._doc.Data[1].Email} `,
};
transporter.sendMail(mail_option, (err, info) => {
if (err) {
console.log(err);
} else {
console.log("Works");
}
});
res.json("Sent");
}
});
Any help would be deeply appreciate