Error: Object doesn't support property or method in angular js code for fb login

437 views Asked by At

I am trying to integrate facebook login for my angular JS website. I am getting a pop up and even I am getting some data like my id and username. The issue what I am facing here is when I am trying to redirect the page or sending this data to a handleUser object in my controller, it is not allowing me to call any of the function from that fbLOgin() function block. It is not allowing me to call any function from dat block... please help me with this.

FBLogin(){

console.log("came into fb ");
FB.init({
  appId            : 'my_id',
  autoLogAppEvents : true,
  xfbml            : true,
  version          : 'v2.9'
    // js.src = "//connect.facebook.net/en_US/sdk.js";
});



FB.login(function(response) {
      if (response.authResponse) {
       console.log('Welcome!  Fetching your information.... ');
       FB.api('/me', function(response) {
//         console.log('Good to see you, ' + response.name + '.');
         console.log(response.name);
     let user = {
       "userAccountId":response.name,
       "userRole": "CUSTOMER",
       "lastOtpRequest": 0,
       "fullName": "myname",
       "mobileNo": "1234567890",
       "emailId": "[email protected]",
       "like": false,
       "token": 0,
       "loyal": false,
       "guest": false,
       "enabled": true,
       "newCustomer": true
     }

   });
  } else {
   console.log('User cancelled login or did not fully authorize.');
  }

}); }

1

There are 1 answers

0
Neeli sarath On BEST ANSWER

Actually I am having the same problem with the gmail account also. So what I found here is, we cant call the current object from that promise. So I Created another function from that promise and I m calling that function. The code is as follows..

googleLogin() {
    var google = window.gapi;
    var currentObject = this;
    function authenticateUser(data) {
        var user = {};
        user.emailId = data.getEmail();
        user.fullName = data.getName();
        user.socialId = data.getId();
        user.username = data.getGivenName();
        currentObject.handleUser(user);
    }

    google.load('auth2', function () {
        google.auth2.init({
            client_id: ''
        }).then(function (auth2) {
            if (auth2.isSignedIn.get()) {
                var googleUser = auth2.currentUser.get();
                authenticateUser(googleUser.getBasicProfile());
            } else {
                auth2.signIn();
                auth2.isSignedIn.listen(onSignIn);
                function onSignIn(googleUser) {
                    console.log(googleUser);
                    if (googleUser) {
                        var googleUser = auth2.currentUser.get();
                        authenticateUser(googleUser.getBasicProfile());
                    }
                }
            }
        });
    });
}