Ionic - Angular - Ionic-native facebook: expected 2 arguments, but got 4

180 views Asked by At

I installed the cordova plugin facebook4 and ionic-native/facebook, but got an error using the api() method of the plugin, which is documented here.

The plugin is functioning well, but I got a TypeScript error:

Typescript Error Expected 2 arguments, but got 4.

On this line:

.then((res: FacebookLoginResponse) => this.fb.api(res.authResponse.userID + "/?fields=id,email,first_name&access_token="+res.authResponse.accessToken+"", ["public_profile"],

According to this example, the api method is taking 4 parameters, the request to the Graph API as well as the fields required, the permission used for the fields, and two success / error callbacks.

I don't know what is going wrong here.

Why is the api() call is only waiting for 2 arguments ?

I used the official example from ionic-native documentation.

Here's my code:

fbLogin(): void {
    var self = this;
    this.fb.login(['public_profile', 'email'])
    .then((res: FacebookLoginResponse) => this.fb.api(res.authResponse.userID + "/?fields=id,email,first_name&access_token="+res.authResponse.accessToken+"", ["public_profile"],
      (result) => {
        let user = new NmUser({
          email: result.email,
          username: result.firstName,
          password: result.id
        });
        self.userService.facebook(user).subscribe(
          (result) => {
            console.log(result);
          },
          (error) => {
            console.log(error);
          }
        );
      }, (error) => {
        console.error("Failed: ", error);
      }
    ))
    .catch(e => console.log('Error logging into Facebook', e));
  }

Does someone has a clue ?

Thanks in advance to anyone that will take the time to read/answer !

1

There are 1 answers

1
Mat-Dob-Dev On BEST ANSWER

You could simply inspect what is going on in

/node_modules/@ionic-native/facebook/index.d.ts

The definition of 'api' method is:

    api(requestPath: string, permissions: string[]): Promise<any>;

Which means it takes exactly two arguments and returns a Promise.

You should modify your code to use yet another .then(...).catch(...) methods in the following way:

fbLogin(): void {
  var self = this;
  this.fb.login(['public_profile', 'email'])
  .then((res: FacebookLoginResponse) =>
  this.fb.api(res.authResponse.userID + "/?fields=id,email,first_name&access_token="+res.authResponse.accessToken+"", ["public_profile"])
 .then((result) => {
    //process fb.api result
  }).catch(e => {
    //process fb.api error
})
)
.catch(e => console.log('Error logging into Facebook', e));

}