将回调转换为异步会引发错误
converting callback to async throws error
以下代码有效:
var smtpConfig = {
host: 'localhost',
port: 465,
secure: true, // use SSL
selfSigned: true
};
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpConfig);
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Some One" <someone@example.com>', // sender address
to: 'validuser@company.com', // list of receivers
subject: 'Hello', // Subject line
text: 'Hello world ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(info);
}
});
但是,如果我使用来自节点
的 util.promisify
var sendMail = promisify(transporter.sendMail);
var info = await sendMail(mailOptions);
我遇到异常
TypeError: Cannot read property 'getSocket' of undefined
at sendMail (c:\Users\user\Source\project\node_modules\nodemailer\lib\mailer\index.js:143:25)
at sendMail (internal/util.js:230:26)
问题出在 sendMail 内部,因为 'this' 未定义:
/**
* Sends an email using the preselected transport object
*
* @param {Object} data E-data description
* @param {Function?} callback Callback to run once the sending succeeded or failed
*/
sendMail(data, callback) {
let promise;
if (!callback && typeof Promise === 'function') {
promise = new Promise((resolve, reject) => {
callback = shared.callbackPromise(resolve, reject);
});
}
if (typeof this.getSocket === 'function') { <-- this is undefined
this.transporter.getSocket = this.getSocket;
this.getSocket = false;
}
在普通函数上使用 util.promisify 是可行的。我想这是因为我在 class 方法上使用了 util.promisify
。
有没有办法实现这个,或者我需要用 async function sendMail
重载
扩展扩展 class
要解决这个特殊情况,您可以使用
await sendMail.call(transporter, mailOptions) // provide a context
当然,查看 transporter.sendMail()
的源代码,它已经支持作为 then
able 调用而无需包装在 util.promisify()
中,因此您可以直接 await
:
await transporter.sendMail(mailOptions) // omit callback to kick in promise-style async
以下代码有效:
var smtpConfig = {
host: 'localhost',
port: 465,
secure: true, // use SSL
selfSigned: true
};
// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport(smtpConfig);
// setup e-mail data with unicode symbols
var mailOptions = {
from: '"Some One" <someone@example.com>', // sender address
to: 'validuser@company.com', // list of receivers
subject: 'Hello', // Subject line
text: 'Hello world ?', // plaintext body
html: '<b>Hello world ?</b>' // html body
};
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
console.log(error);
} else {
console.log(info);
}
});
但是,如果我使用来自节点
的util.promisify
var sendMail = promisify(transporter.sendMail);
var info = await sendMail(mailOptions);
我遇到异常
TypeError: Cannot read property 'getSocket' of undefined
at sendMail (c:\Users\user\Source\project\node_modules\nodemailer\lib\mailer\index.js:143:25)
at sendMail (internal/util.js:230:26)
问题出在 sendMail 内部,因为 'this' 未定义:
/**
* Sends an email using the preselected transport object
*
* @param {Object} data E-data description
* @param {Function?} callback Callback to run once the sending succeeded or failed
*/
sendMail(data, callback) {
let promise;
if (!callback && typeof Promise === 'function') {
promise = new Promise((resolve, reject) => {
callback = shared.callbackPromise(resolve, reject);
});
}
if (typeof this.getSocket === 'function') { <-- this is undefined
this.transporter.getSocket = this.getSocket;
this.getSocket = false;
}
在普通函数上使用 util.promisify 是可行的。我想这是因为我在 class 方法上使用了 util.promisify
。
有没有办法实现这个,或者我需要用 async function sendMail
重载
要解决这个特殊情况,您可以使用
await sendMail.call(transporter, mailOptions) // provide a context
当然,查看 transporter.sendMail()
的源代码,它已经支持作为 then
able 调用而无需包装在 util.promisify()
中,因此您可以直接 await
:
await transporter.sendMail(mailOptions) // omit callback to kick in promise-style async