Angular/Firebase - "UserCredential"类型中不存在"电子邮件"属性

Hri*_*its 0 firebase firebase-authentication angularfire2 angular

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { User } from './../classes/user';
import { AlertService } from './alert.service';
import { Alert } from './../classes/alert';
import { AlertType } from './../enums/alert-type.enum';
import { Observable } from 'rxjs';
import 'rxjs/add/Observable/of';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import 'rxjs/add/operator/switchMap';
import { from } from 'rxjs';
import 'rxjs/add/observable/fromPromise';
import { AngularFireAuthModule } from 'angularfire2/auth';


@Injectable({
  providedIn: 'root'

})
export class AuthService {

  public currentUser: Observable<User | null>;

  constructor(
    private router: Router,
    private alertService: AlertService,
    private afAuth: AngularFireAuth,
    private db: AngularFirestore
  ) {
    // TODO fetch the user from the Firebase backend, then set the user(actioned!)
    this.currentUser = this.afAuth.authState
      .switchMap((user) => {
        if (user) {
          return this.db.doc<User>(`users/${user.uid}`).valueChanges();
        } else {
          return Observable.of(null);
        }
      });
  }
  public signup(firstName: string, lastName: string, email: string, password: string): Observable<boolean> {
    // TODO call Firebase signup function(actioned!)

    return Observable.fromPromise(
      this.afAuth.auth.createUserWithEmailAndPassword(email, password)
        .then((user) => {
          const userRef: AngularFirestoreDocument<User> = this.db.doc(`users/${user.uid}`);
          const updatedUser = {
            id: user.uid,
            email: user.email,
            firstName,
            lastName,
            photoUrl: 'https://avatarfiles.alphacoders.com/131/131718.jpg'
          };
          userRef.set(updatedUser);
          return true;
        })
        .catch((err) => false)
    );
    return Observable.of(true);
  }

  public login(email: string, password: string): Observable<boolean> {
    // TODO call Firebase login function
    return Observable.of(true);
  }

  public logout(): void {
    // TODO call Firebase logout function
    this.router.navigate(['/login']);
    this.alertService.alerts.next(new Alert('You have been signed out.'));
  }

}
Run Code Online (Sandbox Code Playgroud)

嗨 - 我希望有人能帮我解决下面的问题 - 我得到的错误如图所示,这是错误来自的代码片段.我是角度和Firebase的新手,还在学习这些如何工作..

基本上 - 我试图创建一个聊天应用程序 - 代码说 - 我创建一个用电子邮件和密码的用户将解析来自导入的电子邮件和密码.

src/app/services/auth.service.ts中的错误(47,85):错误TS2339:类型'UserCredential'上不存在属性'uid'.src/app/services/auth.service.ts(49,22):错误TS2339:属性'UserCredential'上不存在属性'uid'.src/app/services/auth.service.ts(50,25):错误TS2339:"UserCredential"类型中不存在属性"email".src/app/services/auth.service.ts(60,5):错误TS7027:检测到无法访问的代码.

一切都很完美,除了这些错误让我疯了!

Fra*_*len 5

它看起来像UserCredential没有uid属性.我最好的猜测是你正在寻找user.user.uid.