[英]How to handle an null response.json? (Javascript)
I'm currently trying to handle a NULL response to a REST API我目前正在尝试处理 NULL 对 REST API 的响应
Code:代码:
const { name } = await fetch(`api link`).then(response => response.json());
Now normally if there is an error I would simply use an if(;name) return;
现在通常如果有错误我会简单地使用
if(;name) return;
, but since the response is NULL and not an API response this won't work. ,但由于响应是 NULL 而不是 API 响应,这将不起作用。 I've been trying numerous things but none seem to work.
我一直在尝试很多东西,但似乎都没有奏效。 I would appreciate some help on this or any solutions.
我将不胜感激有关此或任何解决方案的帮助。
I am receiving an UnhandledPromiseRejectionWarning when the response is null.当响应为 null 时,我收到 UnhandledPromiseRejectionWarning。
also response.json() return a promise. response.json() 也返回一个 promise。 try with this:
试试这个:
const response = await fetch(`api link`);
const result = await response.json();
if(result === null || result === undefined) {
// return or do something
}
const { name } = result;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.