TypeError: Cannot read property 'set' of undefined in React native

TypeError: Cannot read property 'set' of undefined in React native

我正在使用 KeyValueStorage 调用异步方法,我从 "react-native-key-value-storage" 导入了 KeyValueStorage 但不知道哪里出了问题。

   import KeyValueStorage from "react-native-key-value-storage"

    onsuccessLogin =async  () => {
        try {
          await KeyValueStorage.set("userId", data.userId);
          await KeyValueStorage.set("token", data.token);
          Actions.drawerMenu();
        } catch(error) {
            // Handle error
            console.log("Login-Error",error);
        }
      };

使用 AsyncStorage 而不是 KeyValueStorage 它与您使用的相同。

但是这里不能传递整数类型的值,必须将它们转换成字符串。

而且很简单。

等待AsyncStorage.setItem('userId', '' + UserId + '');

其中userId是键,UserId是整型值,我们使用 '' 将内部值转换为字符串。

希望它能帮助您并节省您的时间,因为我浪费了两天时间在 AsyncStorage 中传递整数类型值。