Node.js 从一个控制器方法到另一个控制器方法的回调
Node.js callback from one controller method to other
我在我的项目中创建了一个 emailHelper
用于发送电子邮件。
我想在邮件发送成功后收到回电
//emailHelper.js
module.exports = {
sendMail: (options) => {
var transporter = nodemailer.createTransport({
service: 'SendGrid',
auth: {
user: keys.sendGridUsername,
pass: keys.sendGridPassword
}
});
const mailOptions = {
from: options.from,
to: options.to,
subject: options.subject,
html: options.content
};
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
return false;
}
else {
console.log('mail sent');
return true;
}
});
}
}
我只想在发送电子邮件时将数据保存到数据库
const { sendMail } = require('../helpers/emailHelper');
mail: (req, res, next) => {
const emailOptions = {
from: 'from@email.com',
to: 'to@email.com',
subject: 'subject',
content: 'text'
};
const mailResetLink = sendMail(emailOptions);
if(mailResetLink) {
//success save data
} else {
//failure trrow error
}
}
谢谢
将您的 sendMail 包裹在 promise 中,return 它:
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
reject();
}
else {
console.log('mail sent');
resolve();
}
});
});
比你能做到的:
sendMail(emailOptions).then(() => {
// success save data
}).catch(() => {
// failure trrow error
});
使用 async/await 你需要将 promise 更改为 return 一些标识符,在这种情况下是布尔值,然后调整邮件功能:
return new Promise((resolve) => {
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
resolve(false);
}
else {
console.log('mail sent');
resolve(true);
}
});
});
mail: async (req, res, next) => {
const emailOptions = {
from: 'from@email.com',
to: 'to@email.com',
subject: 'subject',
content: 'text'
};
const mailResetLink = await sendMail(emailOptions);
if(mailResetLink) {
//success save data
} else {
//failure trrow error
}
}
我在我的项目中创建了一个 emailHelper
用于发送电子邮件。
我想在邮件发送成功后收到回电
//emailHelper.js
module.exports = {
sendMail: (options) => {
var transporter = nodemailer.createTransport({
service: 'SendGrid',
auth: {
user: keys.sendGridUsername,
pass: keys.sendGridPassword
}
});
const mailOptions = {
from: options.from,
to: options.to,
subject: options.subject,
html: options.content
};
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
return false;
}
else {
console.log('mail sent');
return true;
}
});
}
}
我只想在发送电子邮件时将数据保存到数据库
const { sendMail } = require('../helpers/emailHelper');
mail: (req, res, next) => {
const emailOptions = {
from: 'from@email.com',
to: 'to@email.com',
subject: 'subject',
content: 'text'
};
const mailResetLink = sendMail(emailOptions);
if(mailResetLink) {
//success save data
} else {
//failure trrow error
}
}
谢谢
将您的 sendMail 包裹在 promise 中,return 它:
return new Promise((resolve, reject) => {
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
reject();
}
else {
console.log('mail sent');
resolve();
}
});
});
比你能做到的:
sendMail(emailOptions).then(() => {
// success save data
}).catch(() => {
// failure trrow error
});
使用 async/await 你需要将 promise 更改为 return 一些标识符,在这种情况下是布尔值,然后调整邮件功能:
return new Promise((resolve) => {
transporter.sendMail(mailOptions, (err, info) => {
if(err) {
console.log(err);
resolve(false);
}
else {
console.log('mail sent');
resolve(true);
}
});
});
mail: async (req, res, next) => {
const emailOptions = {
from: 'from@email.com',
to: 'to@email.com',
subject: 'subject',
content: 'text'
};
const mailResetLink = await sendMail(emailOptions);
if(mailResetLink) {
//success save data
} else {
//failure trrow error
}
}