TypeError:app.post(...)。那么它不是一个函数

时间:2018-05-22 10:18:11

标签: mysql node.js express nodemailer

我在节点App中遇到API路由问题。我切换了Nodemailer的'to'部分,现在突然间它在我的'Post'动作中给了我Async的问题。

我一直收到错误消息:'TypeError:app.post(...)。然后不是函数'

以下是代码:

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

1 个答案:

答案 0 :(得分:1)

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

看起来你真的希望你的承诺来自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返回一个承诺,但是如果不知道你正在使用的包,就不可能确定。此外,请注意我添加了“res.end()”,它将在尝试电子邮件代码之前关闭HTTP响应,这是可选的,但应确保客户端在处理电子邮件之前获得响应。您可能也可能不想这样做。