小编Abh*_*ats的帖子

类型“string |”上不存在属性“_id” JwtPayload'。类型“string”上不存在属性“_id”

正如您在 NodeJS 代码中看到的,我正在从 URL 中提供的 Postman 的令牌中获取用户,但我正在努力从给定的令牌编号中取回 _id 属性。我想要的是已解码的 _id 中的 id,以便我可以在进一步的操作中使用该 id

const jwt = require('jsonwebtoken')
const User = require('../model/users')
const auth = async (req, res, next) => {
    try {
          const token = req.header('Authorization').replace('Bearer', '')
          const decoded = jwt.verify(token, 'thisisfromabhishek')

`the property _id does not exist on decoded`
          const user = await User.findOne({ _id: decoded._id, 'tokens.token': token })

        if (!user) {
            throw new Error()
        }
        req.token = token
        req.user = user
        next()
       } catch (e) {
        res.status(401).send({ error: …
Run Code Online (Sandbox Code Playgroud)

javascript mongodb node.js jwt

12
推荐指数
1
解决办法
1万
查看次数

标签 统计

javascript ×1

jwt ×1

mongodb ×1

node.js ×1