【问题标题】:'User' is not a assignable to type 'void'“用户”不能分配给“无效”类型
【发布时间】:2019-02-19 03:30:39
【问题描述】:

几个月来我一直在使用 Ionic 4 和 Firestore。一旦我去了 Firestore 和 Ionic 4,很多东西都坏了,所以我从头开始。我正在关注适用于 iOS 和 Android Youtube 的 AngularFirebase Ionic Google 登录教程,但出现以下错误:

[ng] src/app/pages/login/login.page.ts(35,7) 中的错误:错误 TS2322:类型“用户”不可分配给类型“无效”。

我使用与教程中完全相同的代码,但不知道为什么会出现此错误。

    import { Component, OnInit } from '@angular/core';
    import * as firebase from 'firebase/app';
    import { AngularFireAuth } from 'angularfire2/auth';
    import { Observable, of } from 'rxjs';
    import { GooglePlus } from '@ionic-native/google-plus/ngx';
    import { environment } from '../../../environments/environment';

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss'],
})
export class LoginPage  {

  user: Observable<firebase.User>;

  constructor(private afAuth: AngularFireAuth,
               private gplus: GooglePlus) {

        this.user = this.afAuth.authState;
      }
  googleLogin() {
    this.nativeGoogleLogin();
  }

    async nativeGoogleLogin(): Promise<void> {
      try {

      const gplusUser = await this.gplus.login({
        'webClientId': environment.googleWebClientId,
        'offline': true,
        'scopes': 'profile email'
      });

           return await this.afAuth.auth.signInWithCredential(
         firebase.auth.GithubAuthProvider.credential(gplusUser.idToken)
      );

          } catch (err) {
              console.log(err);
          }
        }
      }

【问题讨论】:

  • 行号是什么:35 ??是firebase.auth.GithubAuthProvider.credential(gplusUser.idToken)??
  • 是的。它使用上面 7 行收到的令牌来使用该令牌登录。

标签: javascript angular firebase firebase-authentication ionic4


【解决方案1】:

signInWithCredential 正在返回一个 firebase.User 对象,因此您可以将函数的返回类型更改为 Promise&lt;firebase.User&gt;。

async nativeGoogleLogin(): Promise<firebase.User> {
      try {

      const gplusUser = await this.gplus.login({
        'webClientId': environment.googleWebClientId,
        'offline': true,
        'scopes': 'profile email'
      });

           return await this.afAuth.auth.signInWithCredential(
         firebase.auth.GithubAuthProvider.credential(gplusUser.idToken)
      );

          } catch (err) {
              console.log(err);
          }
        }
      }

【讨论】:

    【解决方案2】:

    我从来没有写过一行 Angular,所以请耐心等待。但是我写了一些打字稿。

    您(或本教程的作者)将此 Promise 的返回类型设置为 void,如果登录成功则返回用户,如果登录失败则返回 void

    async nativeGoogleLogin(): Promise&lt;void&gt; { ... }

    这会返回某种类型的用户对象

    return await this.afAuth.auth.signInWithCredential(
       firebase.auth.GithubAuthProvider.credential(gplusUser.idToken)
    );
    

    尝试将方法的返回值更新为 User 或 void(用于捕获)

    async nativeGoogleLogin(): Promise<firebase.User | void> {
        try {
    
        const gplusUser = await this.gplus.login({
            'webClientId': environment.googleWebClientId,
            'offline': true,
            'scopes': 'profile email'
        });
    
        return await this.afAuth.auth.signInWithCredential(
            firebase.auth.GithubAuthProvider.credential(gplusUser.idToken)
        );
    
        } catch (err) {
            console.log(err);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2022-01-21
      • 2020-07-13
      • 2021-07-02
      • 2021-08-27
      • 2022-01-05
      • 1970-01-01
      • 2021-12-10
      • 2022-01-27
      相关资源
      最近更新 更多