Fetch error when calling response.json()

被刻印的时光 ゝ 提交于 2019-12-25 03:09:05

问题


In my API class, I have a code like below,

let response;
try {
  response = await fetch(requestUrl, {
    method: 'POST',
    headers,
    timeout: REQUEST_TIMEOUT_MS,
    body: JSON.stringify({
      id,
      accNumber: acctId,
      invNumber: invId,
      fromDate,
      toDate
    })
  });
} catch (error) {
  throw new Error(
    'Unexpected error occurred while invoking ATOM Invoice API',
    error
  );
}
foo(cloneDeep(response));
const result = await response.json();
--- some more code ---

Then in another class, I have implemented foo method,

async foo(response){
    let result;
    let errorDescription;
    try {
      result = await response.json();
    } catch (error) {
      logger.error(JSON.stringify(error));
    }
    --- some more code ---
}

When running the above code I get the following error thrown from foo function:

{
    "name": "FetchError",
    "message": "response timeout at <my request URL in here> over limit: 10000",
    "type": "body-timeout"
}

I would like to know why this happens and what should I do to avoid this. Thanks in advance!

来源:https://stackoverflow.com/questions/50460134/fetch-error-when-calling-response-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!