简体   繁体   English

TypeError:app.post(…)。然后不是一个函数

[英]TypeError: app.post(…).then is not a function

I am having some problems with my API routes in a node App. 我在节点App中的API路由遇到一些问题。 I switched the 'to' portion of my Nodemailer and now all of a sudden it is giving me problems with the Async in my 'Post' Action. 我将Nodemailer的“ to”部分切换了一下,现在突然之间,“ Post”操作中的Async出现了问题。

I keep getting an error mesage: 'TypeError: app.post(...).then is not a function' 我不断收到错误消息: 'TypeError:app.post(...)。然后不是函数'

Here is the code: 这是代码:

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

It seems you've attached a .then promise handler to app.post . 看来你已经附上.then承诺处理程序app.post app.post is provided by Express and does not return a promise, instead it consumes a handler function. app.post由Express提供,不返回promise,而是使用处理程序函数。

It looks like you actually intended for your promise to come from db.Applicant.create instead. 看起来您实际上是希望从db.Applicant.create中获得承诺。 In this case you will need to take your .then promise and put it following the db.Applicant.create like this. 在这种情况下,你需要把你的.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...
    });
  });
});

In this case I'm assuming that db.Applicant.create returns a promise, though it is not possible to know for sure without knowing the packages you're using. 在这种情况下,我假设db.Applicant.create返回一个db.Applicant.create ,尽管在不知道您使用的软件包的情况下无法确切知道。 Also, notice that I added "res.end()" which will close the HTTP response before attempting the email code, this is optional but should ensure the client gets a response before the email is processed. 另外,请注意,我添加了“ res.end()”,它将在尝试发送电子邮件代码之前关闭HTTP响应,这是可选的,但应确保客户端在处理电子邮件之前获得响应。 You may, or may not want to do this as well. 您可能也可能不想这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM