Monday, June 3, 2024
64
rated 0 times [  65] [ 1]  / answers: 1 / hits: 24531  / 7 Years ago, mon, may 22, 2017, 12:00:00

I'm trying to check whether a key is available in AsyncStorage with AsyncStorage.getItem('key_name'). If the key is not available it is not returning null, it still returns following promise object:



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


My function for getting data is as bellow:



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
}
}


How can I check whether a key exists in AsyncStorage or not?


More From » react-native

 Answers
35

You need to add async await, or add .then to the result


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
}
}

[#57703] Friday, May 19, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aleenamarinr

Total Points: 610
Total Questions: 109
Total Answers: 118

Location: Mayotte
Member since Mon, Sep 12, 2022
2 Years ago
aleenamarinr questions
Thu, Sep 23, 21, 00:00, 3 Years ago
Mon, Dec 28, 20, 00:00, 4 Years ago
Sat, Dec 14, 19, 00:00, 5 Years ago
;