正如您在 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)