John
John

Reputation: 41

unexpected result from validation function in React Native

I was doing a simple validation for registering new users in React Native, I am doing this by firstly doing regex validation which work as intended but when I check whether the username or email is already stored in the AsyncStorage, I get a weird output when I log it, this is the output:

{"_U": 0, "_V": 0, "_W": null, "_X": null}

The expected output is: new

My validation function is:

//this function checks whether the provided email or username exists in the storage
const user_check = async (type,val)=>{
    try {
        //get the list of users from the storage
        const user_obj = await AsyncStorage.getItem('users'); 

        //if the email is being checked, get a list of all emails and see if it is in there
        if(type==="email" && (user_obj != null)){
            const email_list = user_obj.users.map(u=>u.email)
            for(i in email_list){
                if(email_list[i]===val){
                    //return "exists" if the email is found in the list
                    return "exists";
                }
            }
            //return new if the email is not found in the list
            return "new";
        } 
        //if the username is being checked, get a list of all usernames and see if it is there
        else if(type==="username" && (user_obj != null)){
            const username_list = user_obj.users.map(u=>u.username)
            for(i in username_list){
                if(email_list[i]===val){
                    //return "exists" if the username is found in the list
                    return "exists";
                }
            }
            //return "new" if the username is not found in the list
            return "new";
        }
        //this is for when no users are registered yet
        if((type==="email" || type==="username") && (user_obj == null)){
            return "new";
        }
        //return null if an unkown type is provided
        return null;
    }
    catch(e){
        alert(e)
    }
}

And this is function that calls it:

    //this function registers the user after validating the username, password and email
    async register(username,email,password){
        //if the username,email or password are invalid display an alert with the reason
        if(validate.validation("username",username)==="invalid"){
            Alert.alert("invalid username", "make sure that the chosen username is between 1 and 15 letters and only contains numbers")
        }else if(validate.validation("email",email)==="invalid"){
            Alert.alert("invalid email", "Please provide a valid email")
        }else if(validate.validation("password",password)==="invalid"){
            Alert.alert("invalid password","make sure the password is atleast 8 characters long")
        }

        //check if the username,email and password are all valid
        if(validate.validation("username",username)==="valid" && 
           validate.validation("email",email)==="valid" &&
           validate.validation("password",password)==="valid"){
                //if the username and email are both not stored in the local storage then store the new user and send the user to the sign in screen 
                if(validate.user_check("username",username)==="new" && validate.user_check("email",email)==="new"){
                    new_user = {username,email,password, user_id: username + Date.now()}

                    store.storeUser(new_user);
                    this.props.navigation.navigate('Sign In')
                } 
                //if the username already exists display an alert informing the user
                else if(validate.user_check("username",username)==="exists"){
                    Alert.alert("username already taken", "please choose another username");
                }
                //if the email already exists display an alert informing the user
                else if(validate.user_check("email",email)==="exists"){
                    Alert.alert("email already registered", "this email is already registered, you might want to login instead");
                }
                Alert.alert("test",console.log(validate.user_check("username",username)))
           }
    }

Upvotes: 0

Views: 182

Answers (1)

Nitin Sidhu
Nitin Sidhu

Reputation: 161

{"_U": 0, "_V": 0, "_W": null, "_X": null}

this output is a promise which has to be resolved . before Using anywhere. try using await or .then before it.

Upvotes: 1

Related Questions