Mysql TypeError:app.post(…)。then不是函数

Mysql TypeError:app.post(…)。then不是函数,mysql,node.js,express,nodemailer,Mysql,Node.js,Express,Nodemailer,我在节点应用程序中的API路由有一些问题。我切换了nodeEmailer的“to”部分,现在它突然给我的“Post”操作中的异步带来了问题 我一直收到一个错误消息:'TypeError:app.post(…)。then不是函数' 代码如下: app.post("/api/applicants", function(req, res) { db.Applicant.create(req.body); res.send('Successfully posted new Applicant')

我在节点应用程序中的API路由有一些问题。我切换了nodeEmailer的“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...
      }); 
   });
app.post(“/api/applicators”),函数(req,res){
db.applicator.create(请求主体);
res.send(“已成功发布新申请人”);
}).then(函数(appPost){
//nodeEmailer的邮件详细信息
让邮件选项={
发件人:'”noreply@cap.org“,//发件人地址
致:'ijvv7dth54f7zp3w@ethereal.email“,//接收者列表
主题:'已提交申请',//主题行
text:req.body.firstname+''+req.body.last_name+'刚刚给您发送了一条消息!',//纯文本正文
html:''+req.body.first_name+''+'
'+ ''+req.body.last_name+'
'+ “多布:” //html正文 }; transporter.sendMail(邮件选项,(错误,信息)=>{ 如果(错误){ 返回console.log(错误); } console.log('发送的消息:%s',info.messageId); //预览仅在通过Ethereal帐户发送时可用 log('预览URL:%s',nodeEmailer.getTestMessageUrl(信息)); //已发送的消息: //预览URL:https://ethereal.email/message/WaQKMgKddxQDoou... }); });
您似乎已将一个
附加到
应用程序。post
app.post
由Express提供,不返回承诺,而是使用处理函数

看起来您实际上希望您的承诺来自db.applicator.create。在这种情况下,您需要接受您的
。然后
承诺,并将其放在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...
    });
  });
});
app.post(“/api/applicators”),函数(req,res){
返回db.applicator.create(请求主体),然后返回函数(appPost){
//响应HTTP请求
res.send(“已成功发布新申请人”);
res.end();//确保在尝试发送任何电子邮件之前发送响应
//nodeEmailer的邮件详细信息
让邮件选项={
发件人:'”noreply@cap.org“,//发件人地址
致:'ijvv7dth54f7zp3w@ethereal.email“,//接收者列表
主题:'已提交申请',//主题行
text:req.body.firstname+''+req.body.last_name+'刚刚给您发送了一条消息!',//纯文本正文
html:''+req.body.first_name+''+'
'+ ''+req.body.last_name+'
'+ “多布:” //html正文 }; transporter.sendMail(邮件选项,(错误,信息)=>{ 如果(错误){ 返回console.log(错误); } console.log('发送的消息:%s',info.messageId); //预览仅在通过Ethereal帐户发送时可用 log('预览URL:%s',nodeEmailer.getTestMessageUrl(信息)); //已发送的消息: //预览URL:https://ethereal.email/message/WaQKMgKddxQDoou... }); }); });

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

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

看起来您实际上希望您的承诺来自db.applicator.create。在这种情况下,您需要接受您的
。然后
承诺,并将其放在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...
    });
  });
});
app.post(“/api/applicators”),函数(req,res){
返回db.applicator.create(请求主体),然后返回函数(appPost){
//响应HTTP请求
res.send(“已成功发布新申请人”);
res.end();//确保在尝试发送任何电子邮件之前发送响应
//nodeEmailer的邮件详细信息
让邮件选项={
发件人:'”noreply@cap.org“,//发件人地址
致:'ijvv7dth54f7zp3w@ethereal.email“,//接收者列表
主题:'已提交申请',//主题行
text:req.body.firstname+''+req.body.last_name+'刚刚给您发送了一条消息!',//纯文本正文
html:''+req.body.first_name+''+'
'+ ''+req.body.last_name+'
'+ “多布:” //html正文 }; transporter.sendMail(邮件选项,(错误,信息)=>{ 如果(错误){ 返回console.log(错误); } console.log('发送的消息:%s',info.messageId); //预览仅在通过Ethereal帐户发送时可用 log('预览URL:%s',nodeEmailer.getTestMessageUrl(信息)); //已发送的消息: //预览URL:https://ethereal.email/message/WaQKMgKddxQDoou... }); }); });

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

太棒了。你能解释一下为什么必须在db.create前面使用“return”吗?在这个上下文中,它不是“必需的”,但假设你想在第一个承诺中再加上一个承诺。然后你会“return”一个承诺。例如,您可以在发送电子邮件后执行
返回db.applicator.update
。你会的”