gpt4 book ai didi

javascript - Passport.js——如何异步使用渲染电子邮件

转载 作者:行者123 更新时间:2023-12-02 21:14:53 24 4
gpt4 key购买 nike

我正在使用 Sequelize/MariaDB 和 Passport.js 运行 Express 来进行用户身份验证。

我处于注册部分(有效),但我似乎无法呈现并返回要求他们确认电子邮件的激活电子邮件。

passport.js(包含身份验证策略)

passport.use('user-signup-email', new LocalStrategy({
//Process/validate input, check database for existing emails, create user, add to database...

...
if (newUser) {
var token = jwt.sign( { data: newUser.id }, newUser.login_pass + "-" + newUser.account_created);
var URLdata = {
id: newUser.id,
expiration_time: Date.now() + 86400000, //24 hours
url: token,
email_info: mail.createActivationEmail(req.app, newUser.login_key, newUser.user_alias, token)
};

console.log("info: " + URLdata.email_info);

...
//Store dynamic URL and email contents (in case of resending) in database with expiration time
//And then send the email that was just rendered
}

mail.js

exports.createActivationEmail = (app, recipientAddress, userName, url) => {
app.render('emails/activation_email', {
layout: false,
page_title: 'Please confirm your account!',
dynamic_url: url
}, function(err, rendered) {
if (err) {
console.log("Q [" + err + "]");
}

console.log("R " + rendered.toString());
return {
from: adminEmailAddress,
to: recipientAddress,
cc: false,
bcc: false,
subject: 'Welcome to example.com ' + userName + '!',
html: rendered.toString(),
text: "TO DO"
};
});
};

passport.js 中的最后一个 console.log 显示“信息:未定义”。但如果我在返回之前打印 mail.js 模块中的输出,那就没问题了。

我猜这是一个异步问题?我该如何解决它?
我对这种情况下的 promise 和异步等待 block 仍然有点不清楚。

预先感谢您提供的任何帮助!

最佳答案

您误解了回调函数。回调(当你编写它们时应该)是异步的: https://nemethgergely.com/async-function-best-practices/ How to write asynchronous functions for Node.js

我更改了您的 createActivationEmail 函数。最后一个参数现在是一个回调,当您的代码 app.redner 完成时会调用它。

passport.use('user-signup-email', new LocalStrategy({
//Process/validate input, check database for existing emails, create user, add to database...

// ...
if(newUser) {

var token = jwt.sign({ data: newUser.id }, newUser.login_pass + "-" + newUser.account_created);
mail.createActivationEmail(req.app, newUser.login_key, newUser.user_alias, token, (err, email_info) => {



var URLdata = {
id: newUser.id,
expiration_time: Date.now() + 86400000, //24 hours
url: token,
email_info
};


console.log("info: " + URLdata.email_info);

//...
//Store dynamic URL and email contents (in case of resending) in database with expiration time
//And then send the email that was just rendered


});

}

}));

exports.createActivationEmail = (app, recipientAddress, userName, url, done) => {
app.render('emails/activation_email', {
layout: false,
page_title: 'Please confirm your account!',
dynamic_url: url
}, function(err, rendered) {

if (err) {
console.log("Q [" + err + "]");
cb(err);
return;
}

console.log("R " + rendered.toString());

done(null, {
from: adminEmailAddress,
to: recipientAddress,
cc: false,
bcc: false,
subject: 'Welcome to example.com ' + userName + '!',
html: rendered.toString(),
text: "TO DO"
});

});
};

关于javascript - Passport.js——如何异步使用渲染电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60996924/

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