这是我构建的一个简单的电子邮件爆炸程序我陷入了循环接收数据的困境。我想打印结果中的所有数据,但我可以打印结果中的最后一个数据。
这是我的代码:
var mailer = require("nodemailer"),
config = require('./config'),
redshiftClient = require('./redshift.js'),
sql = require('./main');
var smtpTransport = mailer.createTransport({
service: "Outlook",
auth: {
user: config.user,
pass: config.passwd
}
});
console.log("Application started at : "+ new Date().getTime())
sql.log();
redshiftClient.query(getQuery())
.then(function (data) {
console.log("Query executed at " + new Date().getTime())
var element = data.rows[index];
for (let index = 0; index < data.rows.length; index++) {
var element = data.rows[index];
var mail = {
from: '[email protected]',
to: '[email protected]',
subject: `Daily AWS validation check for the ${new Date().getTime()}`,
html: `<h1> Hello Team, Here is the hourly data pull</h1> <br> <table><tr> <th>Hoursdata</th> <th>Book_Counts</th> <th>Search_Counts</th> <th>Hit_Counts</th> <tr><td>${element.run_hour}</td> <td>${element.book_counts}</td> <td>${element.search_counts}</td> <td>${element.hit_counts}</td></tr> </table>`
}
smtpTransport.sendMail(mail, function (error, response) {
if (error) {
console.log(error);
} else {
console.log("Email sent");
}
smtpTransport.close();
});
}
})
.catch(function (err) {
console.error(err);
});
redshiftClient.close();
代码的我的输出:(给出了最后一个值)
Hoursdata Book_Counts Search_Counts Hit_Counts
2 2131 5645654 4355
预期输出:(我需要所有值)
Hoursdata Book_Counts Search_Counts Hit_Counts
1 23425 457474 2534
2 2131 5645654 4355
[您想要做的是,遍历所有行,并为每一行发送一条消息,而您应该使用html中的所有详细信息创建一封邮件。如我先前的回答中所建议,请使用一些npm包创建html并将其传递给html,同时创建消息,然后再发送电子邮件。