首页 > 解决方案 > LocalStrategy 的函数参数在哪里获取它的参数,或者如何使异步函数中的变量在另一个函数中可用?

问题描述

我的代码:

const LocalStrategy = require('passport-local').Strategy;
//called some hash modules

function initialize(passport, getUserByEmail, getUserById) {
  var user_copy;
  const authenticateUser = async(email, password, done) => {
    const userr = await getUserByEmail(email).then(function(userr) {
      return userr
    })

    user_copy = userr; //I can't reach userr so I create copy of it on accessible variable.

    if (userr == null) {
      return done(null, false, {
        message: "No user with this email..."
      });
    }

    try {
      //hashing part
    } catch (e) {
      console.log(e);
      return done(e);
    }
  }

  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  }, authenticateUser));
 
 
  /////////////////////Problem/////////////////////
  passport.serializeUser((user_copy, done) => done(null, user_copy[0]._id)); //In here I get error( at the under line too). It see user_copy undefined. 
  passport.deserializeUser((id, done) => {
    return done(null, getUserById(user_copy[0]._id));
  })
}
  /////////////////////////////////////////////////
 

module.exports = initialize

实际上,如果我能够在框外调用 getUserByEmail 就没有问题,但我不知道 authenticateUser 从哪里获取电子邮件参数,所以我尝试在 authenticateUser 中完成我的工作,但找不到解决方案。

我在哪里调用初始化:

initializePassport(
    passport,
    async function email(email) {
        await client.connect();
        const db = client.db(dbName);
        const collection = db.collection(collectionName);

        found = await collection.find({ email: email }).toArray().then(user => { return user });

        client.close();

        return found;
    },
    async function id() {
        await client.connect();
        const db = client.db(dbName);
        const collection = db.collection(collectionName);

        found = await collection.find({ id: user.id }).toArray().then(user => { return user });

        client.close();

        return found;
    }
);
如您所见,有电子邮件参数,但它从哪里获取?

我只想能够访问 userr 或它的任何副本。我尝试了太多方法,但找不到任何方法。我控制一切的另一件事没有问题。

标签: javascriptnode.jsasynchronousasync-awaitglobal-variables

解决方案


您应该做的是将user_copy变量分配给的返回值getUserByEmail

const LocalStrategy = require('passport-local').Strategy;
//called some hash modules

function initialize(passport, getUserByEmail, getUserById) {
  var user_copy;
  const authenticateUser = async(email, password, done) => {
    user_copy = await getUserByEmail(email).then(function(userr) {
      return userr
    })
// this wouldn't be needed
    //user_copy = userr; //I can't reach userr so I create copy of it on accessible variable.

    if (user_copy == null) {
      return done(null, false, {
        message: "No user with this email..."
      });
    }

    try {
      //hashing part
    } catch (e) {
      console.log(e);
      return done(e);
    }
  }

  passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  }, authenticateUser));
 
 
  /////////////////////Problem/////////////////////
  passport.serializeUser((user_copy, done) => done(null, user_copy[0]._id)); //In here I get error( at the under line too). It see user_copy undefined. 
  passport.deserializeUser((id, done) => {
    return done(null, getUserById(user_copy[0]._id));
  })
}
  /////////////////////////////////////////////////
 

module.exports = initialize


推荐阅读