【发布时间】:2019-03-28 12:47:59
【问题描述】:
如何在 Asyncstorge 中设置多个值?因为我正在尝试设置 token 和 user_id 。这些是服务器响应值。
这就是响应的样子
json
{ error: 0,
data: 'User registered Successfully',
userData:
{ pwd: 'lmlmlm',
phone_no: '9898989',
user_name: '',
status: 1,
date: 2018-10-24T07:12:20.656Z },
user_id: 60,
token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.' }
在用户注册后,我在 react native 中写过这样的代码。
.then((response) => response.json())
.then((responseData) =>
{
console.log(responseData);
AsyncStorage.setItem('id_token', responseData.token),
AsyncStorage.setItem('user_id', responseData.user_id),
Actions.firstScreen();
});
在我的主页中,我正在访问AsyncStorage 中的token 和user_id
AsyncStorage.getItem('id_token').then((usertoken) =>{
this.setState({'id_token' : usertoken});
console.log(usertoken);
})
AsyncStorage.getItem('user_id').then((uid) => {
this.setState({'user_id': uid});
console.log(uid);
})
但我得到的只是令牌,uid 的安慰结果为 null。但是当我安慰 responseData 时,我里面有值。请帮助我
【问题讨论】:
-
我不知道这是否会成为问题,但我会像这样进行访问:
Promise.all([AsyncStorage.getItem('id_token'), AsyncStorage.getItem('user_id')]).then(([id_token, usertoken]) => { this.setState({id_token, user_id}); console.log(id_token, user_id); });
标签: javascript json react-native asyncstorage