【问题标题】:Ionic - Angular - Ionic-native facebook: expected 2 arguments, but got 4Ionic - Angular - Ionic-native facebook:预期 2 个参数,但得到 4 个
【发布时间】:2018-12-24 11:07:23
【问题描述】:

我安装了 cordova 插件 facebook4 和 ionic-native/facebook,但使用插件的 api() 方法时出错,记录在 here

插件运行良好,但出现 TypeScript 错误:

Typescript 错误预期有 2 个参数,但得到了 4 个。

在这一行:

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

根据this的例子,api方法有4个参数,对Graph API的请求以及需要的字段,字段使用的权限,以及两个成功/错误回调。

我不知道这里出了什么问题。

为什么 api() 调用只等待 2 个参数?

我使用了official example from ionic-native documentation

这是我的代码:

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));
  }

有人有线索吗?

提前感谢任何愿意花时间阅读/回答的人!

【问题讨论】:

    标签: angular facebook-graph-api ionic-framework ionic-native cordova-facebook


    【解决方案1】:

    你可以简单地检查发生了什么

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

    'api'方法的定义是:

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

    这意味着它只需要两个参数并返回一个 Promise。

    您应该修改您的代码以通过以下方式使用另一个 .then(...).catch(...) 方法:

    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));
    

    }

    【讨论】:

    • 过去 5 小时我一直在寻找这个答案。非常感谢!
    猜你喜欢
    • 2020-10-02
    • 1970-01-01
    • 2018-05-29
    • 2019-11-04
    • 2023-04-01
    • 2021-03-03
    • 1970-01-01
    • 2020-10-12
    • 2018-05-20
    相关资源
    最近更新 更多