angular - TS2570 错误协助:“Promise”类型上不存在属性“sendEmailVerification”'。您是否忘记使用“等待”?
问题描述
我正在尝试在我的 Ionic Angular 项目上设置 Firebase。我正在使用链接教程https://www.positronx.io/ionic-firebase-authentication-tutorial-with-examples/来执行此操作。
我不断收到 TS2570 错误:
“Promise”类型不存在属性“sendEmailVerification”。您是否忘记使用“等待”?
我附上了我的代码。我唯一改变的主要事情是 AngularFire 改变了 6.0.0 中使用身份验证的方式
import { Injectable, NgZone } from '@angular/core';
import { auth } from 'firebase/app';
import { User } from "./user";
import { Router } from "@angular/router";
import { AngularFireAuth } from "@angular/fire/auth";
import { AngularFirestore, AngularFirestoreDocument } from '@angular/fire/firestore';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
userData: any;
constructor(
public afStore: AngularFirestore,
public ngFireAuth: AngularFireAuth,
public router: Router,
public ngZone: NgZone
) {
this.ngFireAuth.authState.subscribe(user => {
if (user) {
this.userData = user;
localStorage.setItem('user', JSON.stringify(this.userData));
JSON.parse(localStorage.getItem('user'));
} else {
localStorage.setItem('user', null);
JSON.parse(localStorage.getItem('user'));
}
})
}
// Login in with email/password
SignIn(email, password) {
return this.ngFireAuth.signInWithEmailAndPassword(email, password)
}
// Register user with email/password
RegisterUser(email, password) {
return this.ngFireAuth.createUserWithEmailAndPassword(email, password)
}
// Email verification when new user register
SendVerificationMail() {
return this.ngFireAuth.currentUser.sendEmailVerification()
.then(() => {
this.router.navigate(['verify-email']);
})
}
// Recover password
PasswordRecover(passwordResetEmail) {
return this.ngFireAuth.sendPasswordResetEmail(passwordResetEmail)
.then(() => {
window.alert('Password reset email has been sent, please check your inbox.');
}).catch((error) => {
window.alert(error)
})
}
// Returns true when user is looged in
get isLoggedIn(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return (user !== null && user.emailVerified !== false) ? true : false;
}
// Returns true when user's email is verified
get isEmailVerified(): boolean {
const user = JSON.parse(localStorage.getItem('user'));
return (user.emailVerified !== false) ? true : false;
}
// Sign in with Gmail
GoogleAuth() {
return this.AuthLogin(new auth.GoogleAuthProvider());
}
// Auth providers
AuthLogin(provider) {
return this.ngFireAuth.signInWithPopup(provider)
.then((result) => {
this.ngZone.run(() => {
this.router.navigate(['dashboard']);
})
this.SetUserData(result.user);
}).catch((error) => {
window.alert(error)
})
}
// Store user in localStorage
SetUserData(user) {
const userRef: AngularFirestoreDocument<any> = this.afStore.doc(`users/${user.uid}`);
const userData: User = {
uid: user.uid,
email: user.email,
displayName: user.displayName,
photoURL: user.photoURL,
emailVerified: user.emailVerified
}
return userRef.set(userData, {
merge: true
})
}
// Sign-out
SignOut() {
return this.ngFireAuth.signOut().then(() => {
localStorage.removeItem('user');
this.router.navigate(['login']);
})
}
}
唯一一次调用它是在此处的注册页面中
signUp(email, password){
this.authService.RegisterUser(email.value, password.value)
.then((res) => {
// Do something here
this.authService.SendVerificationMail()
this.router.navigate(['verify-email']);
}).catch((error) => {
window.alert(error.message)
})
}
}
这些是项目中使用的依赖项:
"@angular/common": "~8.2.14",
"@angular/core": "~8.2.14",
"@angular/fire": "^6.0.0",
"@angular/forms": "~8.2.14",
"@angular/platform-browser": "~8.2.14",
"@angular/platform-browser-dynamic": "~8.2.14",
"@angular/router": "~8.2.14",
"@capacitor/cli": "^2.0.1",
"@capacitor/core": "^2.0.1",
"@capacitor/ios": "^2.0.1",
"@ionic-native/core": "^5.0.7",
"@ionic-native/splash-screen": "^5.0.0",
"@ionic-native/status-bar": "^5.0.0",
"@ionic/angular": "^5.0.0",
"core-js": "^2.5.4",
"firebase": "^7.14.0",
"rxjs": "~6.5.1",
"tslib": "^1.9.0",
"zone.js": "~0.9.1"
如果有人可以给我有关如何解决此问题的任何提示?我不是 100% 确定如何在不显示更多错误的情况下实现 async 和 await 函数。
解决方案
新版本 AngularFire 6.0.0 发布以支持 Angular 9。它不向后兼容旧版本。它不再支持旧版本的 Angular (<9)。它还放弃了对旧版本 Typescript(<3.6.4)、Firebase Javascript SDK (<7.13.1) 和 firebase-tools(<8.0.0) 的支持。
AngularFireAuth 的“auth”属性已被弃用,因此“currentUser”的用法也已更改。
'currentUser' 是解决当前用户的承诺。属性“sendEmailVerification”不存在,但可以通过已解析的用户轻松访问。
// Email verification when new user register
SendVerificationMail() {
return this.ngFireAuth.currentUser.then(u => u.sendEmailVerification())
.then(() => {
this.router.navigate(['verify-email']);
})
}
推荐阅读
- python - 仅当特定用户登录时,python 是否可以设置为执行功能?
- r - 频率为 365.25 的时间序列未在更新的 R 版本中运行任何预测包功能
- c# - C# 动态动作生成
- javascript - 无法隐藏由 javascript 创建的 div
- scala - 仅从 Spark Scala DataFrame 写入标头 CSV 记录
- bpmn - Camunda BPMN/CMMN:访问历史用户任务和表单数据
- ldap - 角色不适用于 (LDAP) Active Directory
- mongodb - mongo shell - 转到匹配的括号/花括号
- ssrs-2012 - SSRS 报告显示两个单独的日期条目。需要日期然后与该日期相关的所有详细信息
- python - 属性设置器方法在类创建时不起作用