[英]how to solve circular structure in node.js
i want to use cryptojs to encrypt cookies , i want to encrypt user id with his network card mac address. 我想使用cryptojs来加密 cookie ,我想使用他的网卡mac地址来加密用户ID。
cryptoJS needs to JSON.stringify this data then encrypts. cryptoJS需要JSON.stringify此数据然后进行加密。
but i see this error in console: 但我在控制台中看到此错误:
TypeError: Converting circular structure to JSON
this is a snippet of my code: 这是我的代码的片段:
var mac = 'e0:23:e3:46:57:52'
var userInfo = JSON.stringify({_id: user._id, mac: mac})
var cookie = crypto.AES.encrypt(userInfo, 'secret key')
res.cookie('userInfo', cookie, { expires: new Date(Date.now() + 900000000) })
user._id
is ObjectId
, just convert it to string: user._id
是ObjectId
,只需将其转换为字符串即可:
var userInfo = JSON.stringify({ _id: user._id.toString(), mac: mac });
Also try to use util.inspect
, it automatically replaces circular links with [Circular]
. 也可以尝试使用util.inspect
,它会自动用[Circular]
替换循环链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.