gpt4 book ai didi

javascript - 使用 Node.js 将对象转换为字符串时出现类型错误

转载 作者:行者123 更新时间:2023-12-02 13:45:12 25 4
gpt4 key购买 nike

尝试使用 Node.js/JavaScript 将对象转换为字符串时出现以下错误:

TypeError: Converting circular structure to JSON
at Object.stringify (native)
at exports.userlogin (/opt/lampp/htdocs/FGDP/route/route.js:11:19)
at Layer.handle [as handle_request] (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/layer.js:95:5)
at next (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/layer.js:95:5)
at /opt/lampp/htdocs/FGDP/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/index.js:330:12)
at next (/opt/lampp/htdocs/FGDP/node_modules/express/lib/router/index.js:271:10)

我在下面提供我的代码:

exports.userlogin=function(req,res){
var username=req.body.user_name;
var password=req.body.user_pass;
var ciphertext = CryptoJS.AES.encrypt(password, 'lexelPass');
var pass=JSON.stringify(ciphertext);
}

这里我得到ciphertext变量数据类型是对象,我需要它转换成字符串。但在这里我遇到了这种类型的错误。

最佳答案

CryptoJS.AES.encrypt(password, 'lexelPass') 语句返回的对象是 Circular 类型。圆形类型类似于

var obj = {};
obj.obj = obj;

因此,为了解决这个问题,我们可以利用 JSON.stringify 中的替换回调函数作为第二个参数。通过将丢弃循环对象的逻辑放入替换函数中。

check syntax at MDN for replacer

关于javascript - 使用 Node.js 将对象转换为字符串时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41460640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com