首页 > 解决方案 > Angular / Firebase - 类型“UserCredential”上不存在属性“电子邮件”

问题描述

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

}

嗨 - 我希望有人可以帮助我解决以下问题 - 我收到一个错误,如图所示,这是错误来自的一段代码。我是 Angular 和 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:检测到无法访问的代码。

一切正常,除了这些错误让我发疯!

标签: angularfirebasefirebase-authenticationangularfire2

解决方案


好像UserCredential没有uid属性。我最好的猜测是您正在寻找user.user.uid.


推荐阅读