async function getTokenFromAsync() {
const userToken = await AsyncStorage.getItem('@User_Token');
return userToken;
}
export default {getTokenFromAsync};
Im trying to get userToken from asyncStorage but it return me this {"_U": 0, "_V": 0, "_W": null, "_X": null}, Im using react native
async function getTokenFromAsync() {
const userToken = await AsyncStorage.getItem('@User_Token');
return userToken;
}
export default {getTokenFromAsync};
Im trying to get userToken from asyncStorage but it return me this {"_U": 0, "_V": 0, "_W": null, "_X": null}, Im using react native
Share Improve this question asked Dec 9, 2021 at 7:42 ijaz bachaijaz bacha 1081 silver badge9 bronze badges 2-
What are you storing in key
@User_Token
. – Ravi Commented Dec 9, 2021 at 7:43 - sir im storing user jwt token in asyncStorage but in the above function i want to get the token and return them – ijaz bacha Commented Dec 9, 2021 at 7:44
2 Answers
Reset to default 7You are not resolving the Promise hence your "weird" output.
This is the way that I have my get Function defined:
const getData = async (key) => {
// get Data from Storage
try {
const data = await AsyncStorage.getItem(key);
if (data !== null) {
console.log(data);
return data;
}
} catch (error) {
console.log(error);
}
};
You can then get the Data by calling the Function with your Key.
await getData("yourKey")
.then(data => data)
.then(value => {
a state or constant = value
console.log("yourKey Value: " + value)
})
.catch(err => console.log(err))
You should wait to resolve the promise where you are using it like
getTokenFromAsync().then((userToken)=>{
console.log(userToken);
});