简体   繁体   English

反应本机异步函数返回空对象

[英]React native async function returns empty object

I am trying to get a token stored in securestore, and pass it to url.我正在尝试获取存储在 Securestore 中的令牌,并将其传递给 url。 When I try to call the api the value of the token is just an empty object.当我尝试调用 api 时,令牌的值只是一个空对象。 I am able to see the value t here if I alert in readToken function.如果我在 readToken 函数中发出警报,我可以在这里看到值 t。 Its not passed to getMovies function.它没有传递给 getMovies 函数。

  const readToken = async () => {
    try {
      const storedToken = await SecureStore.getItemAsync('token');
      return storedToken;
    } catch (e) {
      console.log(e);
    }
  };

  const getMovies = async () => {
    try {
       let url = 'https://someurl.de/api/getschichten/?userid=9&token='+readToken();
       alert(url);
      const response = await fetch(url);
      const json = await response.json();
      setData(json);
    } catch (error) {
      console.error(error);
    } finally {
      setLoading(false);
    }
  }

So, I changed my code according to kipnodels.所以,我根据 kipnodels 更改了我的代码。 Here's my code that works这是我的工作代码

  let myToken = await readToken();
  let url = 'https://esm.nvii-dev.de/api/getschichten/?userid=9&token='+myToken;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM