Javascript react native中的AsyncStorage.getItem不工作

Javascript react native中的AsyncStorage.getItem不工作,javascript,react-native,asyncstorage,Javascript,React Native,Asyncstorage,我正在使用AsyncStorage.setItem在服务器上设置数据,并尝试使用AsyncStorage.getItem在另一个屏幕中访问该值。 1.我无法在异步存储中设置令牌。 2.如何在asyncstorage中设置多个值,例如我希望服务器响应中的用户id与此令牌一起设置 onPressRegister(){ fetch('http://192.168.1.7:3000/users/registration',{ method:'PO

我正在使用
AsyncStorage.setItem
在服务器上设置数据,并尝试使用
AsyncStorage.getItem
在另一个屏幕中访问该值。 1.我无法在
异步存储
中设置
令牌
。 2.如何在asyncstorage中设置多个值,例如我希望服务器响应中的
用户id
与此
令牌一起设置

onPressRegister(){

            fetch('http://192.168.1.7:3000/users/registration',{
                method:'POST',
                headers:{
                    'Accept': 'applictaion/json',
                    'Content-Type': 'application/json',

                },
                body:JSON.stringify({
           contact:this.state.contact,
                     password:this.state.password,
                })
            })
            .then((response) => response.json())
            .then((responseData) =>
            {
                 this.setState({
                     signup: responseData
                 });
                 setTimeout(() => {
                    Actions.firstScreen();
                }, 2300);

        AsyncStorage.setItem('token' ,this.state.signup.token);
            });

    }
而我就是这样的

  componentDidMount(){
    AsyncStorage.getItem('token').then((userid) =>{
    this.setState({'userid': userid});
    console.log(userid);
    });
}
{ error: 0,
  data: 'User registered Successfully',
  userData:
   { pwd: 'vgh',
     phone_no: '5',
     user_name: '',
     status: 1,
     date: 2018-10-23T09:23:53.671Z },
  user_id: [  { user_id: 5 } ],
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' }
但是我在
控制台中看不到结果
什么都没有得到。
AsyncStorage.setItem
中是否有错误?此外,服务器响应如下所示

  componentDidMount(){
    AsyncStorage.getItem('token').then((userid) =>{
    this.setState({'userid': userid});
    console.log(userid);
    });
}
{ error: 0,
  data: 'User registered Successfully',
  userData:
   { pwd: 'vgh',
     phone_no: '5',
     user_name: '',
     status: 1,
     date: 2018-10-23T09:23:53.671Z },
  user_id: [  { user_id: 5 } ],
  token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' }

我还希望在
AsyncStorage
中设置
user\u id
。如何做到这一点

我认为您的问题来自setState的async属性

如果您的responseData是一个对象(使用console.log(typeof responseData)测试它), 试试看:

AsyncStorage.setItem('token' , responseData.token);
实际上,setState方法需要一段时间才能有效。React native会在最佳时刻设置状态,而不是在代码中编写时(异步函数)。它不像变量集

另一件要知道的事情是当您调用getItem时。如果您在setItem之后(甚至在导航到另一个屏幕之后)调用它,则无法设置它(相同的异步原因)。然后您可以这样做以确保您的项目已设置:

AsyncStorage.setItem('token' , responseData.token, () => { ...the next instructions (like navigation function) }); 

它是在setItem方法末尾调用的回调

U可以使用
async
wait
从异步存储访问值

async componentDidMount(){
    let token = await AsyncStorage.getItem('token');
    console.log(token);
}

为什么不
AsyncStorage.setItem('token',responseData.token)
我找不到这个链接我想它的意思是@JaromandaX实际上我把响应设置为一个状态变量,这样我就可以使用它了ryt?嗯,是的,我只是说
responseData.token
this.state.signup.token要短得多,而且比
this.state.signup.token>更明显-它对结果没有影响-当然,除非
this.setState
碰巧是异步的(我想是这样的)我试过像这样
AsyncStorage.setItem('token',responseData.token,()=>{Actions.firstScreen();})
。但是没有结果首先你必须尝试静态数据存储ex:AsyncStorage.setItem('token','test'));而不是尝试获取所以我在使用
getItem
之前应该使用
setItem
吗?即使我在另一个页面中设置了
token
?请帮助。我在早期项目中使用了
AsyncStorage
,当时也很简单。唯一的区别是我在导航中使用了
react导航
我正在使用的pjct
react native router flux