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

标签 javascript node.js express async-await passport.js

我正在使用 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/

相关文章:

javascript - 如何将 Angular 4 集成到 Rails 应用程序中?Rails 中 Angular 的实际文件夹结构是什么?

javascript - 绑定(bind)多个点击事件的更短语法 - javascript

javascript - 如何以自定义模式切片数组?

javascript - discord.js v13 permissions.has() 函数不工作(TypeError : Cannot read properties of undefined (reading 'has' ))

javascript - AngularJS SPA : How to Angular Controller SPA to call Angular service?

typescript - 使用 passport.serializeUser 时如何修复 "No overload matches this call."错误?

javascript - 更新对象(_User)并稍后检索它后,字段未更新(解析)

node.js - 如何找到 Mongoose 查询中缺失的架构字段

node.js - 闭包查找node.js、mongodb、express

javascript - 通过值查找 javascript 中对象的索引