簡體   English   中英

如何處理 null 響應。json? (Javascript)

[英]How to handle an null response.json? (Javascript)

我目前正在嘗試處理 NULL 對 REST API 的響應

代碼:

const { name } = await fetch(`api link`).then(response => response.json());

現在通常如果有錯誤我會簡單地使用if(;name) return; ,但由於響應是 NULL 而不是 API 響應,這將不起作用。 我一直在嘗試很多東西,但似乎都沒有奏效。 我將不勝感激有關此或任何解決方案的幫助。

當響應為 null 時,我收到 UnhandledPromiseRejectionWarning。

response.json() 也返回一個 promise。 試試這個:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM