gpt4 book ai didi

javascript - Nodemailer - cb 不是一个函数吗?

转载 作者:行者123 更新时间:2023-11-30 23:54:30 26 4
gpt4 key购买 nike

我创建了一个使用nodemailer发送电子邮件的函数,但运行后我的控制台抛出了我:

TypeError: cb is not a function
at tryHandleCache (C:\Users\Maciek\Desktop\GoParty\backend\node_modules\ejs\lib\ejs.js:226:12)
at Object.exports.renderFile (C:\Users\Maciek\Desktop\GoParty\backend\node_modules\ejs\lib\ejs.js:437:10)
at Object.fn (C:\Users\Maciek\Desktop\GoParty\backend\api\controllers\user\create.js:47:28)
at <anonymous>
at process._tickDomainCallback (internal/process/next_tick.js:229:7)

我的 sendEmails.js 函数

const transporter = require('nodemailer').createTransport(sails.config.custom.email)

module.exports = {
inputs:{
to: { type:'string', required:true },
subject: { type:'string', required:true},
html: {type:'string', required:true}
},
exits:{
success: {
description: 'All done.'
}
},

fn: async function(inputs, exits){
const options = {
from: sails.config.custom.email.auth.user,
to: inputs.to,
subject: inputs.subject,
html: inputs.html
}

transporter.sendMail(options, (err, info) => {
if(err){
return exits.error(err)
}else return exits.success(info.response)
})
}
}

我的create.js,我必须在其中发送包含正确变量的电子邮件:


const ejsVariable = {
activeCode: inputs.activateCode
}
// const html = await ejs.renderFile(templatePath, ejsVariable)
// const subject = 'EventZone - potwierdzenie rejestracji'
// const res = await sails.helpers.email.sendEmail(inputs.email, subject, html)
// if(!res){
// return this.res.badRequest('Confirmation email has not been send.')
// }

感谢您的帮助

最佳答案

ejs.renderFile 有 4 个参数,最后一个是一个函数。用法示例:

ejs.renderFile(filename, data, options, function(err, str){
// str => Rendered HTML string
});

它不会返回 promise ,因此您无法等待它。

尝试更换

const html = await ejs.renderFile(templatePath, ejsVariable)
const subject = 'xxx'
const res = await sails.helpers.email.sendEmail(inputs.email, subject, html)

ejs.renderFile(templatePath, ejsVariable, async (err, html) => {
const subject = 'xxx'
const res = await sails.helpers.email.sendEmail(inputs.email, subject, html)
})

更新

您可以使用util.promisify使ejs.renderFile函数返回一个promise,从而像这样使用async wait:

const util = require('util') //first import `util`

....

const asyncEjsRenderFile = util.promisify(ejs.renderFile)
const html = await asyncEjsRenderFile(templatePath, ejsVariable)
const subject = 'xxx'
const res = await sails.helpers.email.sendEmail(inputs.email, subject, html)

关于javascript - Nodemailer - cb 不是一个函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61147837/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com