Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何在catch语句中获得正确的错误?_Reactjs_Typescript_Http_Fetch - Fatal编程技术网

Reactjs 如何在catch语句中获得正确的错误?

Reactjs 如何在catch语句中获得正确的错误?,reactjs,typescript,http,fetch,Reactjs,Typescript,Http,Fetch,我使用它来处理HTTP 这是我如何处理请求的一个示例: 在这里,我有一个问题,在catch语句中,我希望从后端获得error对象,但我得到了这个错误: HTTPError at _callee$ (webpack-internal:///./node_modules/ky/index.js:367:23) at tryCatch (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:63:17)

我使用它来处理HTTP

这是我如何处理请求的一个示例:

在这里,我有一个问题,在
catch
语句中,我希望从后端获得error对象,但我得到了这个错误:

HTTPError
    at _callee$ (webpack-internal:///./node_modules/ky/index.js:367:23)
    at tryCatch (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:63:17)
    at Generator.invoke [as _invoke] (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:282:22)
    at Generator.prototype.<computed> [as next] (webpack-internal:///./node_modules/regenerator-runtime/runtime.js:116:21)
    at asyncGeneratorStep (webpack-internal:///./node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:5:24)
    at _next (webpack-internal:///./node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js:27:9)

谢谢你的帮助

此错误对象是由ky.js创建的客户端错误。 如果后端创建错误并将其打印到响应正文,则需要读取响应正文以获取错误消息。我不知道ky.js,但我想它是:

error.response.text.then(text=>console.log(text));

此错误对象是由ky.js创建的客户端错误。 如果后端创建错误并将其打印到响应正文,则需要读取响应正文以获取错误消息。我不知道ky.js,但我想它是:

error.response.text.then(text=>console.log(text));

执行了console.log,并得到如下响应:
ReadableStream{locked:false}
,因为响应主体不是字符串,而是ReadableStream。您需要读取这个流来获得结果(想象一个10GB的响应,它甚至不能适应内存)工作,就像这样:api.post('contract',{json})。然后(()=>closeAllocationModal()).catch(async err=>{const{message}=wait err.response.json();setAllocationError(message);});谢谢你的帮助!执行console.log,并获得如下响应:
ReadableStream{locked:false}
,因为响应主体不是字符串,而是ReadableStream。您需要读取这个流来获得结果(想象一个10GB的响应,它甚至不能适应内存)工作,就像这样:api.post('contract',{json})。然后(()=>closeAllocationModal()).catch(async err=>{const{message}=wait err.response.json();setAllocationError(message);});谢谢你的帮助!
{
  "compilerOptions": {
    "target": "es2015",
    "module": "esnext",
    "jsx": "preserve",
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true
  },
  "include": ["src", "types"]
}