Search code examples
javascriptreactjsreact-nativeasyncstorage

AsyncStorage always returns {"_U": 0, "_V": 0, "_W": null, "_X": null}


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


Solution

  • 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))