javascript - JWT 解码返回“[object Object]”
问题描述
我正在尝试解码发送到我的其余 API 服务器的 JSON Web 令牌。但是,当我尝试使用在网络令牌中发送的 _id 属性时,我无法让它工作。这是我正在使用的代码:
jwt.verify(token, process.env.TOKEN_SECRET, { complete: true }, async (err, decoded) => {
console.log(decoded)
if (err) {
res.status(400).json({
error: 'Token is not valid'
});
return
} else {
// Verify user exists
const userExists = await User.findById(decoded._id);
if (userExists == [] || !userExists){
res.status(400).json({
error: 'Token is not valid'
});
return
} else {
req.decoded = decoded;
next();
}
}
});
这是我得到的控制台日志:
[object Object]
这是记录的错误:
(node:4) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '_id' of null
我将不胜感激!
解决方案
而不是像这样记录信息:
console.log(decoded)
你应该这样做:console.log(JSON.stringify(decoded))
您应该在控制台中很好地格式化您的对象。
推荐阅读
- python-3.x - Pyarrow 表从现有列创建列
- c - C中指针向量的内存分配
- text-rendering - 使用 Harfbuzz 正确组合字符位置
- c# - 使用 Sockets 或 UdpClient 通过 LAN 广播 UDP 数据报时无法接收已知回复
- algorithm - 插入排序和合并排序的最坏和最好的运行时间是多少?
- javascript - 如何在不修改子项的情况下操作父项(css/react)
- javascript - React ag grid 条件单元格渲染器
- mysql - 节点 mysql 无法获得查询响应
- sql - Godaddy服务器中asp.net web api中的连接字符串
- git - 如何在 Git 中修复分离的头部