this.setState inside function inside function returns undefined

61 Views Asked by At

So the Connection popup succeeded and prints all data i need from console but I suppose to put some data on a state as shown below but it saves as undefined and found this error on console TypeError: Cannot read property 'setState' of undefined Here is my code:

Twitter() {
var provider = new firebase.auth.TwitterAuthProvider();
firebase.auth().signInWithPopup(provider).then(function (result) {
            // This gives you a the Twitter OAuth 1.0 Access Token and Secret.
            // You can use these server sides with your app's credentials to access the Twitter API.
            var token = result.credential.accessToken;
            var secret = result.credential.secret;
            // The signed-in user info.
            var user = result.user;
            // ...
            console.log(result)
            document.cookie = "twittertoken=" + token + ";";
            document.cookie = "twittersecret=" + secret + ";";

            username = result.additionalUserInfo.profile.screen_name
            profilepic = user.photoURL

            console.log(username + " " + profilepic)
            this.setState(states => ({
                twitter: {
                    ...states.twitter,
                    authorized: true,
                    username: username,
                    profilepic: profilepic,
                }
            }))
        })
            .catch(function (error) {
                // Handle Errors here.
                var errorCode = error.code;
                var errorMessage = error.message;
                // The email of the user's account used.
                var email = error.email;
                // The firebase.auth.AuthCredential type that was used.
                var credential = error.credential;
                // ...
                console.error(error)
            })
    }
}

Some says I have to put bind(this) after this.setState() but still give me an error TypeError: Cannot read property 'setState' of undefined Any fix?

0

There are 0 best solutions below