[英]React Native possible unhandled Promise rejection
I'm trying to get values from an API using fetch function of React Native. 我正在尝试使用React Native的提取功能从API获取值。 Then label values retrieved from JSON as Millions , Billions ... etc. 然后将从JSON检索的标签值标记为Millions,Billions ...等。
JSON File JSON文件
{
"value": 92060,
"new": 1
}
To Do what I wanted, I used below code. 做我想做的,我用下面的代码。
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;
} }
And getCodedValues()
function, 还有getCodedValues()
函数,
getCodedValues(value)
{
console.log(Math.abs(Number(value)));
}
Before I check millions and billions, iOS simulator started to show an error saying, 在我检查数百万亿美元之前,iOS模拟器开始显示错误,
Undefined is not an Object (evaluating '_reactNative.Math.abs')
I used async and await here to hold functions while data are being captured. 我在捕获数据时使用了async和await在这里等待功能。 But still seems like in getCodedValues
function value
parameter is undefined
? 但是仍然看起来像在getCodedValues
函数中的value
参数是undefined
吗?
Problem 问题
How about this? 这个怎么样? This only uses async/await and doesn't deal with then/catch because when you use await, it waits for fetch to come back with the response. 这仅使用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.