typeerror:app.post(…).then不是函数

vlurs2pr  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(330)

我在节点应用程序中的api路由有一些问题。我切换了nodemailer的“to”部分,现在它突然给我的“post”操作中的异步带来了问题。
我不断收到一个错误消息:“typeerror:app.post(…).then不是函数”
代码如下:

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...
      }); 
   });
kxeu7u2r

kxeu7u2r1#

看来你已经附上了 .then 向…承诺 app.post . app.post 由express提供,不返回承诺,而是使用处理函数。
看起来您实际上打算让您的承诺来自db.applicator.create。在这种情况下,您需要 .then 承诺并把它放在db.applicator.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 返回一个承诺,但是如果不知道您正在使用的包,就无法确定。另外,请注意,我添加了“res.end()”,它将在尝试使用电子邮件代码之前关闭http响应,这是可选的,但应该确保客户端在处理电子邮件之前获得响应。你可能想,也可能不想这么做。

相关问题