react-native - 仅在异步函数中允许等待错误反应本机
问题描述
我是本机反应的新手,并尝试使用将用户对象保存在应用程序存储中await AsyncStorage.setItem('user', res[1].data);
但是我收到错误
消息
handleLogin = async() => {
NetInfo.fetch().then(state => {
if (state.isConnected) {
const {navigate} = this.props.navigation;
const data = {
email: this.state.userName,
password: this.state.password
};
fetch(`${DOMAIN_URL}/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
})
.then((response) => {
const statusCode = response.status.toString();
const data = response.json();
return Promise.all([statusCode, data]);
})
.then((res) => {
if (res[0] == 200) {
await AsyncStorage.setItem('user', res[1].data);
navigate('Home');
}else{
Alert.alert(
res[1].message
);
}
})
.catch((error) => {
console.error(error);
});
}
else {
Alert.alert(
"No Internet!",
"Needs to connect to the internet in order to work. Please connect tablet to wifi and try again.",
[
{
text: "OK",
onPress: () => { }
}
]
);
};
})
};
我做了handleLogin
异步,但它没有解决错误。存储用户对象的正确方法是什么?
解决方案
推荐阅读
- android - Flutter:如何正确读取 JSON 中的字段?
- python - 如何运行 Python".exe" 不是由 >python"myscript.py" 生成的
- azure - Azure EA 部署
- java - 在 Netty 4 中从 ByteBuf 构造 HTTP 块
- sql - 查找 2019 年比 2018 年订购更多的客户 ID
- reactjs - React.js 和 Odoo CORS 和 auth 验证问题
- audiokit - 如何使用 AudioKit / AudioPlayer 反向播放声音/缓冲区?
- android - 如何让键盘显示在 react-native 的所有视图之上?
- javascript - 为什么在循环中使用 html2canvas 没有获取 div 中的所有图像
- python - 我将如何扩展这样的 Pandas DataFrame?