“用户”不能指定为“无效”类型

问题描述 投票:2回答:2

Ionic 4和Firestore已经被困了好几个月了。一旦我去了Firestore和Ionic 4,很多东西都破了,所以我从零开始。我正在关注AngularFirebase Ionic Google登录iOS和Android Youtube教程,我收到以下错误:

[ng]错误在src / app / pages / login / login.page.ts(35,7)中:错误TS2322:类型'用户'不能指定为'void'类型。

我使用与教程中完全相同的代码,但不确定为什么我收到此错误。

    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);
          }
        }
      }
javascript angular firebase firebase-authentication ionic4
2个回答
3
投票

signInWithCredential返回一个firebase.User对象,因此您可以将函数的返回类型更改为Promise<firebase.User>

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

0
投票

我从未写过一条Angular系列,所以请耐心等待。但是我写了一些打字稿。

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

async nativeGoogleLogin(): Promise<void> { ... }

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

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

尝试将方法的返回值更新为User或void(对于catch)

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.