简体   繁体   English

JS promise。一些 node.js 和护照 js。 如何从 promise {{}} 获取内部结构? 请求用户

[英]JS promise. Some node js and passport js. How can I get an internal structure from promise { {} }? req.user

I can't get an item from req.user, when I try to see what's in req.user, it displays:我无法从 req.user 获取项目,当我尝试查看 req.user 中的内容时,它显示:

Promise {
  {
    login: 'login',
    password: '$2b$10$sivL/8KvPYzlN/b24nWNOOa7pd02WX',
    id: 12,
    role: 'user'
  }
}

It turns out that I can output Promise { {} }, but I only need the internal structure, how can I get it for further processing (take for example only the role)?原来我可以 output Promise { {} },但是我只需要内部结构,如何获取进一步处理(仅以角色为例)?

req.user is sets there (According to the guide) req.user在那里设置(根据指南)

const LocalStrategy = require('passport-local').Strategy
const bcrypt = require('bcrypt')

function initialize(passport, getUserByEmail, getUserById) {
  const authenticateUser = async (email, password, done) => {
    const user = await getUserByEmail(email)
    if (user == null) {
      return done(null, false, { message: 'No user with that email' })
    }

    try {
      if (await bcrypt.compare(password, user.password)) {
        return done(null, user)
      } else {
        return done(null, false, { message: 'Password incorrect' })
      }
    } catch (e) {
      return done(e)
    }
  }

  passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser))
  passport.serializeUser((user, done) => done(null, user.id))
  passport.deserializeUser((id, done) => {
    return done(null, getUserById(id))
  })
}

module.exports = initialize

EDIT: After seeing your strategy, I can see the issue is in deserializeUser .编辑:看到你的策略后,我可以看到问题出在deserializeUser中。 It looks like getUserById returns a promise, so you need to get the result first:看起来getUserById返回一个 promise,所以你需要先得到结果:

  passport.deserializeUser(async (id, done) => {
    const user = await getUserById(id);
    return done(null, user);
  })

Previous answer in case it helps someone else:以前的答案,以防它帮助别人:

Your issue is most likely in your login strategy and not where you want to consume req.user .您的问题很可能出在您的登录策略中,而不是您想要使用req.user的地方。

Assuming you're using the LocalStrategy for passport, you return the final resolved user record in the callback like so:假设您使用 LocalStrategy 作为护照,您将在回调中返回最终解析的用户记录,如下所示:

passport.use(new LocalStrategy(
  function(username, password, done) {
    // Example service returning a promise
    UserService.loginWithPassword({ username, password }.then((user) => {
      if (!user) return done(null, false);
      return done(null, user);
    }).catch(error => {
      return done(err);
    });
  }
));

If you don't resolve the promise with await or then , you will end up with a promise for req.user like you have in your question:如果您不使用awaitthen解决 promise 问题,您最终会得到 req.user 的req.user就像您在问题中遇到的那样:

passport.use(new LocalStrategy(
  function(username, password, done) {
    // Example service returning a promise
    const user = UserService.loginWithPassword({ username, password });
    // user will always be a promise here
    if (!user) return done(null, false);
    return done(null, user);
    // now `req.user` is the promise, instead of the user object
  }
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM