最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AsyncStorage always returns {"_U": 0, "_V": 0, "_W": null, &a

programmeradmin0浏览0评论
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

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

Share Improve this question asked Dec 9, 2021 at 7:42 ijaz bachaijaz bacha 1081 silver badge9 bronze badges 2
  • What are you storing in key @User_Token. – Ravi Commented Dec 9, 2021 at 7:43
  • sir im storing user jwt token in asyncStorage but in the above function i want to get the token and return them – ijaz bacha Commented Dec 9, 2021 at 7:44
Add a ment  | 

2 Answers 2

Reset to default 7

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

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

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论