null
null

Reputation: 1092

Function not getting triggered on componentWillMount when refreshing page

Hmmm why whenever i refresh the page validateAccount function does not run anymore? it only runs if i navigate thru router Link

componentWillMount(){
    const { location, toggleGlobalHeader} = this.props;
    const userID = auth.currentUser;
    if(location.code == 404) {
        toggleGlobalHeader();
    }
    this.validateAccount(userID);
}


 validateAccount = (userObj) => {
        if(userObj){
            this.userRef.once('value').then(snap => {
                if(snap.hasChild(userObj.uid)){
                    this.getRole(userObj);
                } else {
                    this.userRef.child(userObj.uid).push({
                        email: userObj.email,
                        role: 'Member'
                    });
                }
            });
        }

Upvotes: 1

Views: 69

Answers (1)

nitte93
nitte93

Reputation: 1840

As far as I know, i don't think you can use arrow function inside class declaration. Arrow function will work inside any other function, but not inside class declaration.

Try changing it to:

validateAccount(userObj) {
        if(userObj){
            this.userRef.once('value').then(snap => {
                if(snap.hasChild(userObj.uid)){
                    this.getRole(userObj);
                } else {
                    this.userRef.child(userObj.uid).push({
                        email: userObj.email,
                        role: 'Member'
                    });
                }
            });
        }

Upvotes: 2

Related Questions