Home > Back-end >  AsyncStorage always returns {"_U": 0, "_V": 0, "_W": null, "_X&qu
AsyncStorage always returns {"_U": 0, "_V": 0, "_W": null, "_X&qu

Time:12-09

async function getTokenFromAsync() {
  const userToken = await AsyncStorage.getItem('@User_Token');
  return userToken;
}

export default {getTokenFromAsync};

Im trying to get userToken from asyncStorage but it return me this {"_U": 0, "_V": 0, "_W": null, "_X": null}, Im using react native

CodePudding user response:

You are not resolving the Promise hence your "weird" output.

This is the way that I have my get Function defined:

  const getData = async (key) => {
    // get Data from Storage
    try {
      const data = await AsyncStorage.getItem(key);
      if (data !== null) {
        console.log(data);
        return data;
      }
    } catch (error) {
      console.log(error);
    }
  };

You can then get the Data by calling the Function with your Key.

  await getData("yourKey")
  .then(data => data)
  .then(value => {
    a state or constant = value
    console.log("yourKey Value:  "   value)
  })
  .catch(err => console.log(err))

CodePudding user response:

You should wait to resolve the promise where you are using it like

getTokenFromAsync().then((userToken)=>{
  console.log(userToken);
});
  • Related