angular - 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:检测到无法访问的代码。
一切正常,除了这些错误让我发疯!
解决方案
好像UserCredential
没有uid
属性。我最好的猜测是您正在寻找user.user.uid
.
推荐阅读
- javascript - 通过 Node-Fetch 检索 OAuth2 令牌
- python-3.x - 用于将数字尾部与任何字符串分开的正则表达式?
- linux - 运行带有两个输入文件的程序的 shell 脚本
- postgresql - HorizontalPodAutoscaler | 设置/更新变量值
- protocol-buffers - GRPC:在浏览器和服务器之间传递带有 proto.Message 字段的 JSON
- tailwind-css - Tailwind CSS:创建自定义类时引用主题颜色
- python - Python PDF水管工多数据读取
- r - 将 stat_summary 用于嵌套函数?
- python - 为什么在奇异容器中安装主目录会破坏 python 导入?
- django - 如何在详细视图中用 slug 替换 pk