gpt4 book ai didi

node.js - 为什么此代码会发送重复的电子邮件?

转载 作者:太空宇宙 更新时间:2023-11-04 02:17:17 25 4
gpt4 key购买 nike

我们在 Azure 上运行一个调度程序,该调度程序每分钟运行一次,并检查数据库以查看需要为我们的应用程序发送哪些电子邮件并发送它们。

这在绝大多数情况下都有效,但偶尔,在几个小时内,调度程序会开始重复发送大多数电子邮件(最多 5 份)。

    query = "select id, email, textbody, htmlbody, subject from emailTable where sent = 0 AND DateSent IS NULL";
mssql.query(query, {
success: function(results) {
for(var i = 0 ; i < results.length; i++)
{
handleItem(results[i]);
}
},
error: function(err) {
console.log("Error : " + err);
}
});

function handleItem(item) {
sendMail(item);
var query = "update emailTable SET sent = 1, DateSent = GETDATE() where id = ?";
mssql.query(query, [item.id]);

}

function sendMail(objMail)
{
sendgrid.send({
to: objMail.email,
from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f5e9f3f4e5e3c6f5e9f3f4e5e3ebe7efeaa8e5e9eb" rel="noreferrer noopener nofollow">[email protected]</a>',
fromname: "Source",
subject: objMail.subject,
text: objMail.textbody,
html: objMail.htmlbody
}, function(err, json) {
if (err) { return console.error(err); }
});
}

当它开始失败时,就像对同一项目多次调用 handleItemsendMail 一样。循环或解释此行为的一般逻辑是否有任何问题?

最佳答案

Try writing the update query in the callback function for sendMail(item)

eg. sendMail(item,function(){
var query = "update emailTable SET sent = 1, DateSent = GETDATE() where id = ?";
mssql.query(query, [item.id]);
});

function sendMail(objMail,cb)
{
sendgrid.send({
to: objMail.email,
from: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="285b475d5a4b4d685b475d5a4b4d45494144064b4745" rel="noreferrer noopener nofollow">[email protected]</a>',
fromname: "Source",
subject: objMail.subject,
text: objMail.textbody,
html: objMail.htmlbody
}, function(err, json) {
if (err) { cb(err) }
cb(null,json)
});
}

关于node.js - 为什么此代码会发送重复的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35136392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com