Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在_onRefresh中使用异步存储?_Javascript_React Native - Fatal编程技术网

Javascript 在_onRefresh中使用异步存储?

Javascript 在_onRefresh中使用异步存储?,javascript,react-native,Javascript,React Native,如何从异步存储中从刷新中提取信息 以下是我正在尝试的: _onRefresh = () => { const token = await AsyncStorage.getItem('access'); // I cannot do this (I know) const access = 'Bearer ' + token; this.setState({ refreshing: true }); axios.get(`htt

如何从异步存储中从刷新中提取信息

以下是我正在尝试的:

_onRefresh = () => {
        const token = await AsyncStorage.getItem('access'); // I cannot do this (I know)
        const access = 'Bearer ' + token;
        this.setState({ refreshing: true });
        axios.get(`http://laundry.test/api/auth/main`, {
            headers: {
                'Authorization': access,
            }
        }).then(res => {
            this.setState({ laundries: res.data.laundries });
            this.setState({ refreshing: false });
        })
    }
我怎样才能完成我想做的事情?我需要拉令牌来访问我的API并获取我需要的,而无需身份验证。。。我什么都不会拉

将您的_onRefresh设置为异步以解决您的问题

_onRefresh = async () => {
  const token = await AsyncStorage.getItem("access");
  const access = "Bearer " + token;
  this.setState({ refreshing: true });
  let response = axios.get(`http://laundry.test/api/auth/main`, {
    headers: {
      Authorization: access,
    },
  });
  this.setState({
    laundries: response.data.laundries,
    refreshing: false,
  });
};

希望这对你有帮助。无需怀疑。

为什么不能执行AsyncStorage.getItem“access”?因为它不会提取信息。它返回一个承诺,您必须等待它。因为您使用了wait AsyncStorage.getItem'access';您必须将_onRefresh声明为async。我知道您在说什么,但它仍然不允许我将async放在_onRefresh前面接受这个答案,忘记了我必须在哪里添加async dumb me试图将它放在_onRefresh前面。谢谢大家!@DanielLogvin别想那件事。我们都会犯错误。