Javascript Uncaught(在promise中)TypeError:error.json不是函数-在销毁错误消息时

Javascript Uncaught(在promise中)TypeError:error.json不是函数-在销毁错误消息时,javascript,json,reactjs,redux,async-await,Javascript,Json,Reactjs,Redux,Async Await,我正在开发React Redux应用程序 在尝试从返回的API错误中销毁错误消息时,我收到了“Uncaught(in promise)TypeError:error.json不是函数”错误。My API返回具有以下结构的错误: { "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1", "title": "One or more validation e

我正在开发React Redux应用程序

在尝试从返回的API错误中销毁错误消息时,我收到了“Uncaught(in promise)TypeError:error.json不是函数”错误。My API返回具有以下结构的错误:

{
  "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "traceId": "|5f13e001-48d7893e3cc8e8ff.",
  "errors": {
    "Code": [
      "'Code' must not be empty."
    ],
    "Name": [
      "'Name' must not be empty."
    ]
  }
}
这是我处理save方法的方式:

  const handleSave = (event) => {
    event.preventDefault();
    setSaving(true);
    saveTeam(team)
      .then(() => {
        showSnackbar("Team was saved successfully");
        history.push("/teams");
      })
      .catch((error) => {
        let errorMessage = error.message.json();
        debugger;

        setErrors({ onSave: error.message }); //<---- In here I want to pass just the list of errors 
        setSaving(false);
      });
  };
顺便说一句,我正在将
错误。消息
传递给
设置错误
,然后我会在屏幕上打印它。这就是我得到的:

{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|5f13e004-48d7893e3cc8e8ff.","errors":{"Code":["'Code' must not be empty."],"Name":["'Name' must not be empty."]}}

所以
error.message
就是我要找的。。。但是我怎样才能破坏它呢?

您得到了错误,因为您的
json
对象没有类型为
function
的属性可以调用 您可以像这样分解json对象

var-jsonob={
“类型”:https://tools.ietf.org/html/rfc7231#section-6.5.1",
“标题”:“发生了一个或多个验证错误。”,
“状态”:400,
“traceId”:“5f13e001-48d7893e3cc8e8ff。”,
“错误”:{
“代码”:[
“'Code'不能为空。”
],
“姓名”:[
“'Name'不能为空。”
]
}
}
const{type,title,status,traceId,errors}=jsonob

log(类型、标题、状态、traceId、错误)
您之所以会收到错误,是因为您的
json
对象没有类型为
函数的属性可调用
您可以像这样分解json对象

var-jsonob={
“类型”:https://tools.ietf.org/html/rfc7231#section-6.5.1",
“标题”:“发生了一个或多个验证错误。”,
“状态”:400,
“traceId”:“5f13e001-48d7893e3cc8e8ff。”,
“错误”:{
“代码”:[
“'Code'不能为空。”
],
“姓名”:[
“'Name'不能为空。”
]
}
}
const{type,title,status,traceId,errors}=jsonob

console.log(类型、标题、状态、traceId、错误)
假设
错误。消息实际上是
JSON
,您应该使用
JSON.parse
JSON
字符串解析为
JavaScript
对象:

const错误={
消息:JSON.stringify({
类型:“https://tools.ietf.org/html/rfc7231#section-6.5.1",
标题:“发生了一个或多个验证错误。”,
现状:400,
traceId:| 5f13e004-48d7893e3cc8e8ff,
错误:{
代码:[“'Code'不能为空。”,
名称:[“'Name'不能为空。”]
}
})
};
console.log(错误消息);
//使用销毁指定从对象中解压缩属性
const{type,title,status}=JSON.parse(error.message);
控制台日志(“”);
控制台日志(“类型”,类型);
控制台日志(“标题”,标题);

控制台日志(“状态”,状态)
假设
错误。消息
实际上是
JSON
,您应该使用
JSON.parse
JSON
字符串解析为
JavaScript
对象:

const错误={
消息:JSON.stringify({
类型:“https://tools.ietf.org/html/rfc7231#section-6.5.1",
标题:“发生了一个或多个验证错误。”,
现状:400,
traceId:| 5f13e004-48d7893e3cc8e8ff,
错误:{
代码:[“'Code'不能为空。”,
名称:[“'Name'不能为空。”]
}
})
};
console.log(错误消息);
//使用销毁指定从对象中解压缩属性
const{type,title,status}=JSON.parse(error.message);
控制台日志(“”);
控制台日志(“类型”,类型);
控制台日志(“标题”,标题);

控制台日志(“状态”,状态)
如果
errors.message
返回您正在显示的对象-
{“type”:…}
,那么您应该使用
JSON.parse
如果它还不是
JavaScript
对象,或者只需为错误消息选择您想要的任何属性,例如,
error.title
您能澄清一下为什么要在出错时执行json()?是什么让您觉得它应该有一个json()方法?如果您只是控制台.log(错误)会发生什么?@AlexanderStaroselsky我想获取错误列表。console.log(error.message)打印一个json格式的字符串好的,如果您选择console.log(error.message),什么会注销?为什么要尝试执行方法json()?@ISHTOMAS您是否尝试过执行
json.parse(error.message)
json
转换为普通的
JavaScript
对象,以便可以获取所需的任何属性?如果
错误。message
返回您正在显示的对象-
{“type”:
,如果它还不是
JavaScript
对象,那么您应该使用
JSON.parse
,或者只为错误消息选择您想要的任何属性,例如
error.title
,您能否澄清为什么要在出错时执行JSON()?是什么让您觉得它应该有一个json()方法?如果您只是控制台.log(错误)会发生什么?@AlexanderStaroselsky我想获取错误列表。console.log(error.message)打印一个json格式的字符串好的,如果您选择console.log(error.message),什么会注销?为什么要执行json()方法?@ISHTOMAS您是否尝试过使用
json.parse(error.message)
json
转换为普通的
JavaScript
对象,以便获取所需的任何属性?
{"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|5f13e004-48d7893e3cc8e8ff.","errors":{"Code":["'Code' must not be empty."],"Name":["'Name' must not be empty."]}}