TypeError: app.post(...).then is not a function

TypeError: app.post(...).then is not a function

我在节点应用程序中的 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...
      }); 
   });

您似乎已将 .then 承诺处理程序附加到 app.postapp.post 由 Express 提供,不 return 承诺,而是使用处理函数。

看起来您实际上是想让您的承诺来自 db.Applicant.create。在这种情况下,您将需要接受您的 .then 承诺并将其放在 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 return 是一个承诺,尽管在不知道您正在使用的软件包的情况下无法确定。另外,请注意我添加了 "res.end()" ,这将在尝试电子邮件代码之前关闭 HTTP 响应,这是可选的,但应确保客户端在处理电子邮件之前得到响应。您可能也可能不想这样做。