myk*_*man 2 javascript json node.js
我正在尝试记录我的错误并向频道发送通知。当我使用 console.log() 时,我会收到错误消息的描述,但当我使用 JSON.strigify 时,它会返回一个空对象。请问我该如何解决这个问题。
当我运行这段代码时:
console.log(err);
console.log("stringify", JSON.stringify(err));Run Code Online (Sandbox Code Playgroud)
我得到这样的回应:
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 {}Run Code Online (Sandbox Code Playgroud)
我需要错误消息,为了使用 axios 成功发送它,我需要 JSON.stringify() 错误消息。
错误对象上的大多数属性都是不可枚举的,因此您需要执行以下操作:
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
}
)))Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
999 次 |
| 最近记录: |