gpt4 book ai didi

mysql - TypeError : app. post(...).then 不是一个函数

转载 作者:行者123 更新时间:2023-11-29 10:13:47 24 4
gpt4 key购买 nike

我在 Node 应用程序中的 API 路由遇到一些问题。我切换了 Nodemailer 的“to”部分,现在突然间它在“Post”操作中给我带来了异步问题。

我不断收到错误消息:'TypeError: app.post(...).then is not a function'

这是代码:

app.post("/api/applicants", function(req, res) {
db.Applicant.create(req.body);
res.send('Successfully posted new Applicant');
}).then(function(appPost){
//mail details for nodemailer
let mailOptions = {
from: '"noreply@cap.org" <app@cap.org>', // sender address
to: 'ijvv7dth54f7zp3w@ethereal.email', // list of receivers
subject: 'Application Submitted', // Subject line
text: req.body.firstname + ' ' + req.body.last_name + ' just sent you a message!', // plain text body
html: '<b>'+req.body.first_name+'</b>' + '</br>' +
'' + req.body.last_name + '</br>' +
'DOB: '
// html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@blurdybloop.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});

最佳答案

您似乎已附加 .thenapp.post promise 处理程序。 app.post由 Express 提供,不返回 Promise,而是使用处理函数。

看起来您实际上打算让您的 promise 来自 db.Applicant.create。在这种情况下,您需要携带 .then Promise 并将其放在 db.Applicant.create 之后,如下所示。

app.post("/api/applicants", function(req, res) {
return db.Applicant.create(req.body).then(function(appPost){
// Respond to the HTTP request
res.send('Successfully posted new Applicant');
res.end(); // Ensure the response is sent before any emailing is attempted

//mail details for nodemailer
let mailOptions = {
from: '"noreply@cap.org" <app@cap.org>', // sender address
to: 'ijvv7dth54f7zp3w@ethereal.email', // list of receivers
subject: 'Application Submitted', // Subject line
text: req.body.firstname + ' ' + req.body.last_name + ' just sent you a message!', // plain text body
html: '<b>'+req.body.first_name+'</b>' + '</br>' +
'' + req.body.last_name + '</br>' +
'DOB: '
// html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: %s', info.messageId);
// Preview only available when sending through an Ethereal account
console.log('Preview URL: %s', nodemailer.getTestMessageUrl(info));
// Message sent: <b658f8ca-6296-ccf4-8306-87d57a0b4321@blurdybloop.com>
// Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou...
});
});
});

在本例中,我假设 db.Applicant.create返回一个 promise ,尽管在不知道您正在使用的包的情况下不可能确定。另外,请注意,我添加了“res.end()”,它将在尝试电子邮件代码之前关闭 HTTP 响应,这是可选的,但应确保客户端在处理电子邮件之前获得响应。您可能也可能不想这样做。

关于mysql - TypeError : app. post(...).then 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465223/

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