19

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?

Mostafiz Rahman
  • 8,169
  • 7
  • 57
  • 74

4 Answers4

19

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
    }
}
Community
  • 1
  • 1
Sagar Khatri
  • 1,004
  • 8
  • 23
  • Good answer, and this has nothing to do with the question, but you should use `const` instead of `let` when the value of the variable wont be reassigned. `let` is best used when the variable may be reassigned but not modified, `const` is used when it is neither reassigned nor modified. – ICW Sep 28 '19 at 22:19
  • @YungGun if it's for me my intention was only to show how to fetch value from asyncstorage and other stuff I just copied from OP's code. Nice point anyway. – Sagar Khatri Sep 30 '19 at 05:03
  • @SagarKhatri Am getting error like this when i use this code " await is a reserved word " > 61 | let value1 = await AsyncStorage.getItem('DriverId'); | ^ 62 | let value2 = await AsyncStorage.getItem('AccessToken'); 63 | console.log(value1 + " "+ value2) 64 | this.props.navigation.navigate('Login'); – Srinivasan Lakshmanan Oct 01 '19 at 07:47
  • @SrinivasanLakshmanan you need to use it with async before the function.. could you share you code please? – Sagar Khatri Oct 10 '19 at 12:58
8

Like the name says, it's async. So you have to:

AsyncStorage.getItem('user')
.then((item) => {
     if (item) {
       // do the damage
     }
});

If needed, you can play either with local state or some application state management library.

Samuli Hakoniemi
  • 18,740
  • 1
  • 61
  • 74
4

AsyncStorage is async... so, you need to call it this way:

checkUserSignedIn(callback){
  AsyncStorage.getItem('user', (err, result) => {
    if (!err && result != null){
        // do something 
    }
    else {
        // do something else
    }
    callback(result);
  });
}
UXDart
  • 2,500
  • 14
  • 12
0

You get a promise as error because you need to have await in your function call too. For example please find my general class and how I call it later in another call.

import AsyncStorage from "@react-native-community/async-storage";

const storageSet = async(key, value) => {
    try {
      await AsyncStorage.setItem(key, value);
    } catch(error) {
      console.log(error);
    }
}

const storageGet = async(key) => {
  try {
       const result = await AsyncStorage.getItem(key);
       console.log(result);
       return result;
    } catch(error) {
      console.log(error);
    }
}

export { storageSet, storageGet };

when I call them note the await keyword

 let value = await storageGet("somekey")
 if (value === null || value === undefined ) {
   //no value set in storage
 }
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148