Javascript 如何将Wait关键字与asyncstorage setitem一起用于服务器响应?
我正在尝试在我的react本机应用程序中使用asyncstorage。问题是,我收到的服务器响应需要一些延迟,因此我想等待响应,然后我想使用responseData.user_id保存在我的应用程序中。我使用nodejs作为后端和mysql db。因此,在用户注册后,我会在编写代码的同时将其插入db另一个获取用户id(PK)的查询。所以这个responseData正在访问客户端,我试图从响应中获取该用户id。所以我写了类似这样的东西Javascript 如何将Wait关键字与asyncstorage setitem一起用于服务器响应?,javascript,react-native,async-await,asyncstorage,Javascript,React Native,Async Await,Asyncstorage,我正在尝试在我的react本机应用程序中使用asyncstorage。问题是,我收到的服务器响应需要一些延迟,因此我想等待响应,然后我想使用responseData.user_id保存在我的应用程序中。我使用nodejs作为后端和mysql db。因此,在用户注册后,我会在编写代码的同时将其插入db另一个获取用户id(PK)的查询。所以这个responseData正在访问客户端,我试图从响应中获取该用户id。所以我写了类似这样的东西 onPressRegister = async () =
onPressRegister = async () => {
try {
let response = await fetch('http://192.168.1.2:3000/users/registration', {
method: 'POST',
headers: {
'Accept': 'applictaion/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
contact: this.state.contact,
password: this.state.password,
})
});
let responseData = await response.json();
if (responseData) {
try {
Action.firstScreen();
await AsyncStorage.setItem('userid', JSON.stringify(responseData.userData.phone_no));
}
catch (e) {
console.log('caught error', e);
}
}
} catch (error) {
console.error(error)
}
}
在我的下一个屏幕中,我像这样访问用户ID,然后像这样传递下一个API调用
getUserId = async () => {
let userId = await AsyncStorage.getItem('userid');
return userId;
}
onPressYes = (workType) => {
this.getUserId().then((userId) => {
this.setState({userId:userId})
})
但这是我犯的错误 试试这个:
onPressRegister = async () => {
try {
let response = await fetch('http://192.168.1.6:3000/users/registration', {
method: 'POST',
headers: {
'Accept': 'applictaion/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
contact: this.state.contact,
password: this.state.password,
})
});
let responseData = await response.json();
if (responseData) {
try {
await AsyncStorage.setItem('userid', JSON.stringify(responseData.user_id));
}
catch (e) {
console.log('caught error', e);
}
}
} catch (error) {
console.error(error)
}
}
要访问某些其他组件中的值,请执行以下操作:
getUserId = async () => {
let userId = await AsyncStorage.getItem('userid');
return userId;
}
componentWillMount() {
this.getUserId().then((userId) => {
console.log(userId);
})
}
您可以正确地
等待AsyncStorage.setItem
。。。那么为什么不让usid=wait AsyncStorage.getItem('userid')如果您不添加try
,您可以看到您的问题所在。`data:'User registed Successfully',userData:{pwd:'mkmkmk',phone_no:'23',User_name:'',status:1,date:2018-10-25T05:31:12.358Z},User_id:16,token:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.}这就是responseData的样子,问题是我可以在asyncstorage中设置token,但不能设置用户id..奇怪,请查看AIKasyncstorage
仅存储字符串值。因此,您应该在setItem
调用中尝试类似于JSON.stringify(responseData.user_id)
的东西。您的原始代码中似乎有一个随机行,声明函数async getCache(userid){…}
。这就是导致输出错误的原因。为什么要在处理代码中间声明一个函数?我可以在状态中保存这些值吗?我怎么做?我希望将此userd_id
传递给另一个API调用。因此,请帮助..我尝试了您的代码,但导航到下一个屏幕不起作用。请帮助状态变量不稳定,如果组件卸载,它们的值将丢失。要永久保存某些数据,您需要AsyncStorage
。一旦从存储器中正确检索到某个API,您就可以将其传递给该API。导航到其他屏幕是一个单独的问题,首先您应该确认userid
是否在同一组件中正确存储和检索。首先存储它,然后获取它的值,看看会发生什么。我已经根据您的编辑了我的代码。但问题是我在注册屏幕中的导航设置在哪里。我使用的是react-native-router-flux。请帮助。我恐怕不太熟悉react native router flux
,因为我只使用了react navigation
。您应该查看它的文档,或者打开一个单独的问题,因为这是一个完全不同的问题。是根据你的处境我能找到的最接近的东西。请仔细检查一下,我相信会有帮助的。
getUserId = async () => {
let userId = await AsyncStorage.getItem('userid');
return userId;
}
componentWillMount() {
this.getUserId().then((userId) => {
console.log(userId);
})
}