0

I am new to react native and trying to save user obejct in application storage using await AsyncStorage.setItem('user', res[1].data);
However I am getting error as enter image description here

handleLogin = async() => {
        NetInfo.fetch().then(state => {
            if (state.isConnected) {

        const {navigate} = this.props.navigation;
        const data = {
            email: this.state.userName,
            password: this.state.password
        };

        fetch(`${DOMAIN_URL}/login`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
        })
            .then((response) => {
                const statusCode = response.status.toString();
                const data = response.json();
                return Promise.all([statusCode, data]);
            })
            .then((res) => {

                if (res[0] == 200) {
                    await AsyncStorage.setItem('user', res[1].data);
                    navigate('Home');
                }else{
                    Alert.alert(
                        res[1].message
                    );
                }
            })
            .catch((error) => {
                console.error(error);
            });

            }
            else {
                Alert.alert(
                    "No Internet!",
                    "Needs to connect to the internet in order to work. Please connect tablet to wifi and try again.",
                    [
                        {
                            text: "OK",
                            onPress: () => { }
                        }
                    ]
                );
            };
        })
    };

I have made the handleLogin async but it doesn't solve the error. What is the correct way to store user obejct?

2
  • await is inside the callback function of .then() that arrow function is not async Commented Nov 17, 2020 at 19:43
  • I made a new async function called and navigateToHome and it worked. Commented Nov 17, 2020 at 19:48

2 Answers 2

1

It is recommended that you use react-native-easy-app , through which you can access any data in AsyncStorage synchronously.

Sample_Hooks

StorageController

Sign up to request clarification or add additional context in comments.

Comments

0
   navigateToHome = async (user) => {
        const { navigate } = this.props.navigation;
        await AsyncStorage.setItem('user', user);
        navigate('Home');
    }

    handleLogin = async() => {
        NetInfo.fetch().then(state => {
            if (state.isConnected) {

      
        const data = {
            email: this.state.userName,
            password: this.state.password
        };

        fetch(`${DOMAIN_URL}/login`, {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(data)
        })
            .then((response) => {
                const statusCode = response.status.toString();
                const data = response.json();
                return Promise.all([statusCode, data]);
            })
            .then((res) => {

                if (res[0] == 200) {
                    navigateToHome(res[1].data);
                }else{
                    Alert.alert(
                        res[1].message
                    );
                }
            })
            .catch((error) => {
                console.error(error);
            });

            }
            else {
                Alert.alert(
                    "No Internet!",
                    "Needs to connect to the internet in order to work. Please connect tablet to wifi and try again.",
                    [
                        {
                            text: "OK",
                            onPress: () => { }
                        }
                    ]
                );
            };
        })
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.