【发布时间】:2018-09-03 07:31:59
【问题描述】:
在 ReactNative 组件中,当我按下按钮时,执行此函数时出现“可能未处理的 Promise Rejection”错误:
async onAdd(item) {
try {
const response = await fetch('url', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
event_id: item.id,
event_type: item.event_type,
})
});
const responseJson = await response.json();
} catch (error) {
error(error);
}
}
我不知道为什么,因为它在 try / catch 块中。
更新
这是错误函数:
function error(value) {
if (console) {
console.error(value)
}
}
【问题讨论】:
-
那么
error(error)很可能会抛出异常,因为error不是函数。 -
你的 'error()' 方法在做什么?如果它抛出一个错误,那么 onAdd 也会。
-
您的代码是否应该对
responseJson执行某些操作?喜欢退货吗?
标签: javascript react-native promise async-await