代码之家  ›  专栏  ›  技术社区  ›  Manspof

使用json.stringify将对象转换为字符串显示空对象

  •  0
  • Manspof  · 技术社区  · 6 年前

    我尝试使用json.stringify将对象转换为字符串,得到空对象。

    console.log('typeof',typeof e,'e value is',e,'json stringify is',json.stringify(e))
    

    尝试打印时的错误消息

    typeofobjecte value iserror:error:a network error(such as timeout,interrupted connection or unreachable host)has occurred. json stringify is

    .

    尝试打印时的错误消息

    类型对象E值为错误:错误:发生了网络错误(如超时、连接中断或无法访问主机)。 JSON字符串化是_

    1 回复  |  直到 6 年前
        1
  •  2
  •   Mark    6 年前

    你的目标 e 是一个错误对象。当你试图把它串起来的时候 {} 在chrome和node中。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)
     }