警告:.then() 仅排除函数
Warning: .then() only excepts functions
在 mean stack / nodejs / mongoose 应用程序中,我有这段代码:
User.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(sendEmail()) // this is my addition
.then(respondWithoutResult(res))
.catch(handleError(res));
函数 sendMail 如下所示:
function sendEmail(body){
var mailOptions = {
from: 'Excited User <admin@blabla.com>',
to: 'johny@gmail.com',
subject: 'Hello',
text: 'body text here'
};
var smtpConfig = {
host: config.mailgun.smtp_host,
port: 465,
secure: true,
auth: {
user: config.mailgun.smtp_user,
pass: config.mailgun.smtp_pass
}
};
var transporter = nodemailer.createTransport(smtpConfig);
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
当我 运行 它时,我得到一个错误:
警告:.then() 仅排除函数但已传递:[object Undefined]
我应该在 sendEmail 中更改什么才能使其与 .then() 一起使用?
因为sendMail
supports promises,可以这么简单:
function sendEmail(body) {
var mailOptions = {
from: 'Excited User <admin@blabla.com>',
to: 'johny@gmail.com',
subject: 'Hello',
text: 'body text here'
};
var smtpConfig = {
host: config.mailgun.smtp_host,
port: 465,
secure: true,
auth: {
user: config.mailgun.smtp_user,
pass: config.mailgun.smtp_pass
}
};
var transporter = nodemailer.createTransport(smtpConfig);
// Return the promise here.
return transporter.sendMail(mailOptions);
}
如果要维护日志记录,请将最后一行替换为:
return transporter.sendMail(mailOptions).then(function(info) {
console.log('Message sent: ' + info.response);
return info;
}, function(error) {
console.log(error);
throw error;
});
编辑:我刚刚注意到 body
参数,我猜这可能是 saveUpdates(req.body))
的结果。如果是这样,您还需要稍微调整一下您的承诺链:
User.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(sendEmail)
.then(respondWithoutResult(res))
.catch(handleError(res));
在 mean stack / nodejs / mongoose 应用程序中,我有这段代码:
User.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(sendEmail()) // this is my addition
.then(respondWithoutResult(res))
.catch(handleError(res));
函数 sendMail 如下所示:
function sendEmail(body){
var mailOptions = {
from: 'Excited User <admin@blabla.com>',
to: 'johny@gmail.com',
subject: 'Hello',
text: 'body text here'
};
var smtpConfig = {
host: config.mailgun.smtp_host,
port: 465,
secure: true,
auth: {
user: config.mailgun.smtp_user,
pass: config.mailgun.smtp_pass
}
};
var transporter = nodemailer.createTransport(smtpConfig);
transporter.sendMail(mailOptions, function(error, info){
if(error){
return console.log(error);
}
console.log('Message sent: ' + info.response);
});
}
当我 运行 它时,我得到一个错误: 警告:.then() 仅排除函数但已传递:[object Undefined]
我应该在 sendEmail 中更改什么才能使其与 .then() 一起使用?
因为sendMail
supports promises,可以这么简单:
function sendEmail(body) {
var mailOptions = {
from: 'Excited User <admin@blabla.com>',
to: 'johny@gmail.com',
subject: 'Hello',
text: 'body text here'
};
var smtpConfig = {
host: config.mailgun.smtp_host,
port: 465,
secure: true,
auth: {
user: config.mailgun.smtp_user,
pass: config.mailgun.smtp_pass
}
};
var transporter = nodemailer.createTransport(smtpConfig);
// Return the promise here.
return transporter.sendMail(mailOptions);
}
如果要维护日志记录,请将最后一行替换为:
return transporter.sendMail(mailOptions).then(function(info) {
console.log('Message sent: ' + info.response);
return info;
}, function(error) {
console.log(error);
throw error;
});
编辑:我刚刚注意到 body
参数,我猜这可能是 saveUpdates(req.body))
的结果。如果是这样,您还需要稍微调整一下您的承诺链:
User.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(sendEmail)
.then(respondWithoutResult(res))
.catch(handleError(res));