Search code examples
facebookangulartypescriptionic2ionic3

ionic facebook login - not waiting for response


I have a simple Facebook login issue with ionic. I have tried Async - Await and several other approaches to get it to wait for response. The login works fine but it will not stop for me to work with the response.

How do I add any wait here?? I know this is callback hell.. I just want to grab a few pieces of info. That is all.

import { Facebook, FacebookLoginResponse } from '@ionic-native/facebook';


 constructor(public navCtrl: NavController,
          public navParams: NavParams,
          public fb: Facebook) {


            doFacebookLogin(){
                let env = this;
                this.fb.login(permissions)
                  .then(function(response){

                        env.fb.api("/me?fields=name", params)
                          .then(function(user) {
                            //
                            // do something here with user data
                            //
                          })  // env.fb.api(

                  }, function(error){
                    console.log(error);
                  });  //  this.fb.login(permissions)
              }

Thanks Phil


Solution

  • You must always use fat arrow function as shown below.Then no callback hell.

    doFacebookLogin(){
                let env = this;
                this.fb.login(permissions)
                  .then((response)=>{
    
                        env.fb.api("/me?fields=name", params)
                          .then((user)=> {
                              // do something here with user data
                           })  
                  }, (error)=>{
                    console.log(error);
                  });
              }