Nizom Askarov
Nizom Askarov

Reputation: 21

react-native async storage is not saving and no errors

Async storage is not saving. error is empty

i try to store deviceId in async storage but function setItem is not working

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()

Upvotes: 2

Views: 822

Answers (1)

getItem is an async function, so you need to add await before like below:

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

Or:

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

But other than that, I think it's not saved when you call it and the way you call the function is wrong.

Upvotes: 2

Related Questions