Express.js使用护照等待 [英] Express.js using await with passport

查看:60
本文介绍了Express.js使用护照等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将mySQL添加到password.js中,以对express.js中的用户进行身份验证,但是似乎无法等待工作.Server.js:

I'm trying to add mySQL to passport.js to authenticate users in express.js, but can't seem to get await working. Server.js:

initializePassport(
  passport,
  function(email) {
    pool.getConnection(function(err, connection) {
      if (err) throw err;
      console.log("Connected!");
      pool.query("SELECT * FROM users WHERE email = ?", email, function (err, result) {
        if (err) throw err;
        return result[0];

        connection.release();
      });
    });
  },
)

护照配置

function initialize(passport, getUserByEmail) {
  const authenticateUser = async (email, password, done) => {
    try {
      const user = await getUserByEmail(email);
      console.log(user)
    } catch (e) {
      return done(e)
    }

现在,它只是为用户打印未定义的内容,然后打印已连接.我不确定为什么等待用户无法正常工作.

Right now it just prints undefined for user, and then prints Connected. I'm not sure why the await user isn't working.

推荐答案

好吧,如果它是 getUserByEmail(),那么它在执行异步操作时不会返回与之相关的诺言,因此,执行 await getUserByEmail()不会等待任何事情.

Well if that's getUserByEmail(), then it doesn't return a promise that is connected to when it's asynchronous operations are done, therefore, doing await getUserByEmail() doesn't wait for anything.

await 仅在等待与要等待的操作相关的promise时有用.由于您甚至都没有在等待诺言,因此 await 没什么用.您需要更改 getUserByEmail(),以便它返回一个与您要等待的异步操作相关的Promise.

await ONLY does something useful if you are awaiting a promise that is connected to the operation you want to await for. Since you aren't even awaiting a promise, that await does nothing useful. You would need to change getUserByEmail() so that it returns a promise that is connected to the asynchronous operation you're trying to wait for.

要使函数返回连接到异步操作的Promise,您需要在该函数中的任何地方都使用基于Promise的异步操作,而不是普通的回调异步操作.这些都是数据库操作,所有现代数据库现在都具有基于promise的界面,因此您真正想要做的是切换 .getConnection() .query() .release()均使用基于承诺的操作.这也将使实现正确的错误处理以及与错误的调用者进行正确的通信变得容易得多.

For a function to return a promise that is connected to the asynchronous operations, you need to use promise-based asynchronous operations, not plain callback asynchronous operations, everywhere in that function. These are all database operations and all modern databases have a promise-based interface now so what you really want to do is to switch .getConnection(), .query() and .release() to all use promise-based operations. This will also make it a lot simpler to implement proper error handling and proper communication back to the caller of errors.

我自己对mysql不太了解,但这是一个一般性的想法.promise接口来自模块 mysql2/promise :

I don't know mysql particularly well myself, but here's a general idea. The promise interface comes from the module mysql2/promise:

const mysql = require('mysql2/promise');
const pool = mysql.createPool({...});

initializePassport(passport, async function(email) {
    let connection;
    try {
        connection = await pool.getConnection();
        console.log("Connected!");
        let result = await pool.query("SELECT * FROM users WHERE email = ?", email);
        return result[0];
    } catch(e) {
        // log the error and the rethrow so the caller gets it
        console.log(e);
        throw e;
    } finally {
        if (connection) {
            connection.release();
        }
    }
});

这篇关于Express.js使用护照等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆