我在判断AsyncStorageAsyncStorage.getItem('key_name')的 keys 是否可用.如果密钥不可用,则不会返回null,它仍会返回以下promise 对象:

Promise
_45:0
_54:null
_65:null
_81:1

我获取数据的功能如下:

checkItemExists(){
    let context = this;
    try {
        let value = AsyncStorage.getItem('item_key');
        if (value != null){
            // do something 
        }
        else {
            // do something else
        }
    } catch (error) {
    // Error retrieving data
    }
}

如何判断异步存储中是否存在密钥?

推荐答案

您需要添加async Wait或add.那么结果呢

async checkUserSignedIn(){
    let context = this;
    try {
       let value = await AsyncStorage.getItem('user');
       if (value != null){
          // do something 
       }
       else {
          // do something else
      }
    } catch (error) {
      // Error retrieving data
    }
}

React-native相关问答推荐

错误:HostBody::get for prop NativeUnimoduleProxy中出现异常- Android虚拟设备(AVD)使用Expo测试React Native App时崩溃

如何在底部标签导航中添加顶部标签

如何在 react-navigation v6 中更改标题高度

redux-observable 您在预期流的地方提供了 undefined

ReactJS toLowerCase 不是一个函数

Undefined 不是判断 this.state.* 的对象

React Native 无法读取 index.android.delta

你能在 Ubuntu 上构建 React Native 应用程序(Android 应用程序)吗?

React Native - 用分机拨打电话号码

try 添加包时出现错误duplicate entry:com/google/android/gms/internal/zzble.class

在配置 react-native-maps 中获取 "supportLibVersion" 、 "playServicesVersion" 和 "androidMapsUtilsVersion" 值

在 React Native 视图上强制 onLayout

如何将捕获的图像与 react-native-camera 一起使用

使用 Jest 的 react-navigation 测试组件

React Native 发送短信

I18nManager.forceRTL 不会在首次应用加载时应用更改

React Native,更改 React Navigation 标题样式

将 async 和 await 与 export const 一起使用

React Native:如何动态更改

StackNavigator中如何改变动画的方向?