[英]React Native possible unhandled Promise rejection
我正在嘗試使用React Native的提取功能從API獲取值。 然后將從JSON檢索的標簽值標記為Millions,Billions ...等。
JSON文件
{
"value": 92060,
"new": 1
}
做我想做的,我用下面的代碼。
async componentDidMount() {
let data = await fetch('https://demo614535421.mockable.io/getData')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
grossRev : this.getCodedValues(responseJson.grossRevenue)
})
})
.catch((error) => {
console.error(error);
});
this.setState({ wowData: true });
return data;
}
還有getCodedValues()
函數,
getCodedValues(value)
{
console.log(Math.abs(Number(value)));
}
在我檢查數百萬億美元之前,iOS模擬器開始顯示錯誤,
Undefined is not an Object (evaluating '_reactNative.Math.abs')
我在捕獲數據時使用了async和await在這里等待功能。 但是仍然看起來像在getCodedValues
函數中的value
參數是undefined
嗎?
問題
這個怎么樣? 這僅使用async / await,並且不處理then / catch,因為當您使用await時,它將等待fetch隨響應返回。
async componentDidMount() {
try {
let response = await fetch('https://demo614535421.mockable.io/getData');
let responseJson = response.json();
this.setState({
grossRev: this.getCodedValues(responseJson.grossRevenue)
})
}catch(err){
console.log(err);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.