[英]React-Native AsyncStorage: TypeError: Cannot read property 'setItem' of undefined
[英]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 它與您使用的相同。
但是您不能在此傳遞整數類型值,您必須將它們轉換為字符串。
這很簡單。
await AsyncStorage.setItem('userId', '' + UserId + '');
其中userId是鍵, UserId是整數類型值,我們使用 '' 將內部值轉換為字符串。
希望它可以幫助您並節省您的時間,因為我浪費了兩天在 AsyncStorage 中傳遞整數類型值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.