Harleen Kaur Arora
Harleen Kaur Arora

Reputation: 2087

Local Storage some time return undefined in react-native

Issue Description::

I'm working on react-native application. I am getting user information from local storage, but some time it's return undefined. This working fine on debug mode, only problem in release mode.

Code::

let currentUser = await AsyncStorage.getItem("CURRENT_USER");
let user = JSON.parse(currentUser);

Version::

"react": "^16.8.4",
"react-native": "^0.58.6"

Upvotes: 0

Views: 138

Answers (1)

LordKiz
LordKiz

Reputation: 643

Your code should return a stored string if you have setItem earlier or null if you didn't. I don't know why the need for JSON.parse. How about this:

async getUser() {
    let user = null;
    AsyncStorage.getItem("CURRENT_USER")
    .then((result) => {
        if (result !== null) {
            user = result;
        }
    });
    return user;
}

Upvotes: 1

Related Questions