首页 > 解决方案 > javascript: JSON.Stringify 不显示整个对象

问题描述

我有一个 axios 请求,并且 catch 中的 err 对象看起来像这样

在此处输入图像描述

但是当我尝试使用检查错误对象时

console.log(JSON.stringify(err, null, 2));

我只看到

{
  "message": "Request failed with status code 400",
  "name": "Error",
  "stack": "Error: Request failed with status code 400\n    at createError (http://localhost:3000/material-dashboard-react/static/js/vendors~main.chunk.js:43013:15)\n    at settle (http://localhost:3000/material-dashboard-react/static/js/vendors~main.chunk.js:43247:12)\n    at XMLHttpRequest.handleLoad (http://localhost:3000/material-dashboard-react/static/js/vendors~main.chunk.js:42487:7)",
  "config": {
    "url": "dj-rest-auth/login/",
    "method": "post",
    "data": "{\"email\":\"admin@test.com\",\"password\":\"admin\"}",
    "headers": {
      "Accept": "application/json",
      "Content-Type": "application/json"
    },
    "baseURL": "http://localhost:8025",
    "transformRequest": [
      null
    ],
    "transformResponse": [
      null
    ],
    "timeout": 0,
    "xsrfCookieName": "XSRF-TOKEN",
    "xsrfHeaderName": "X-XSRF-TOKEN",
    "maxContentLength": -1,
    "maxBodyLength": -1
  }
}

我没有看到 err 对象内的响应对象

如何使JSON.stringify显示整个对象

标签: javascript

解决方案


不要JSON.stringify()错误对象。而只是console.log(err)


推荐阅读