【问题标题】:react-native async storage is not saving and no errorsreact-native 异步存储不保存,也没有错误
【发布时间】:2021-12-20 19:01:17
【问题描述】:

异步存储未保存。错误为空

我尝试将 deviceId 存储在异步存储中,但函数 setItem 不起作用

import AsyncStorage from '@react-native-async-storage/async-storage';

const store = async (uniqueId) => { 
const jsonValue = JSON.stringify(uniqueId)
   try {
      await AsyncStorage.setItem("@user", jsonValue, (e,r)=>{ 
        console.log("error: " ,e) 
        return r
      });
   } catch (e) {
      console.log("error: ", e) 
   }
      console.log("get Item: " , AsyncStorage.getItem("@user"))
}
store()

【问题讨论】:

    标签: javascript react-native asyncstorage


    【解决方案1】:

    getItem是一个async函数,所以你需要在下面添加await

    const user = await AsyncStorage.getItem("@user")
    console.log("get Item: ", JSON.parse(user))
    

    或者:

    AsyncStorage.getItem("@user").then((user)=>console.log(JSON.parse(user)))
    

    但除此之外,我觉得调用的时候没有保存,调用函数的方式不对。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 2022-01-17
      • 1970-01-01
      • 2021-03-18
      • 2021-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多