gpt4 book ai didi

javascript - 当 JSON.stringify 显示空对象但实际上它不为空时如何获取实际内容

转载 作者:行者123 更新时间:2023-12-03 08:18:23 33 4
gpt4 key购买 nike

我正在尝试记录我的错误并向 channel 发送通知。当我使用 console.log() 时,我会收到错误消息的描述,但当我使用 JSON.strigify 时,它会返回一个空对象。请问如何解决这个问题。

当我运行此代码时:

console.log(err);
console.log("stringify", JSON.stringify(err));

我收到这样的回复:

ReferenceError: School is not defined
at /Users/macbookpro/Documents...
at Layer.handle [as handle_request] (/Users/macbookpro/Documents...
at next (/Users/macbookpro/Documents...
at adminAccess (/Users/macbookpro/Documents...
at processTicksAndRejections (node:internal/process/task_queues:96:5)
strinfify {}

我需要错误消息,为了使用 axios 成功发送它,我需要 JSON.stringify() 错误消息。

最佳答案

错误对象的大多数属性是 non-enumerable ,所以你需要做这样的事情:

let error = new Error('nested error message');

console.log(JSON.stringify(error))
console.log(JSON.stringify(Object.assign({},
error,
{ // Explicitly pull Error's non-enumerable properties
name: error.name,
message: error.message,
stack: error.stack
}
)))

关于javascript - 当 JSON.stringify 显示空对象但实际上它不为空时如何获取实际内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68640480/

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