我尝试使用 JSON.stringify 将对象转换为字符串,但得到空对象
console.log('typeof',typeof e,' e value is',e, 'JSON stringify is',JSON.stringify(e))
尝试打印时出现错误消息
typeof object e value is 错误:错误:发生网络错误(例如超时、连接中断或无法访问主机)。 JSON 字符串化 {}
最佳答案
您的对象e
是一个错误对象。当您尝试字符串化时,您会在 Chrome 和 Node.js 中得到 {}
。 Safari 显示更多信息。
let e = new Error("hello")
console.log(typeof e)
console.log(JSON.stringify(e))
您可以通过以下方式测试错误:
let e = new Error("Some error happened")
if (e instanceof Error) {
console.log("Error:", e.message)
}
关于javascript - 使用 JSON.stringify 将对象转换为字符串 显示空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51120967/