TS2570 错误协助:“Promise<User>”类型上不存在属性“sendEmailVerification”。您是不是忘记使用“等待”?

Posted

技术标签:

【中文标题】TS2570 错误协助:“Promise<User>”类型上不存在属性“sendEmailVerification”。您是不是忘记使用“等待”?【英文标题】:Assistance With TS2570 Error: Property 'sendEmailVerification' does not exist on type 'Promise<User>'. Did you forget to use 'await'?TS2570 错误协助:“Promise<User>”类型上不存在属性“sendEmailVerification”。您是否忘记使用“等待”? 【发布时间】:2020-07-27 14:18:56 【问题描述】:

我正在尝试在我的 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 函数。

【问题讨论】:

【参考方案1】:

我认为您应该将.then 回调函数移动到组件中,因为在服务中,您将返回promise

在服务中

  // Email verification when new user register
  SendVerificationMail() 
      return this.ngFireAuth.currentUser.sendEmailVerification()
  

在组件中

signUp(email, password)
    this.authService.RegisterUser(email.value, password.value)
    .then((res) => 
        // Do something here
        this.authService.SendVerificationMail()
        .then(() => 
            this.router.navigate(['verify-email']);
        )
    ).catch((error) => 
        window.alert(error.message)
    )

更新

您可以保持一切原样,并将 async / await 添加到不在服务中的组件中的调用方函数

组件

async signUp(email, password)
  this.authService.RegisterUser(email.value, password.value)
  .then((res) => 
    // Do something here
    await this.authService.SendVerificationMail()
    this.router.navigate(['verify-email']);
  ).catch((error) => 
    window.alert(error.message)
  )

服务

// Email verification when new user register
SendVerificationMail() 
  return this.ngFireAuth.currentUser.sendEmailVerification()
  .then(() => 
    this.router.navigate(['verify-email']);
  )

【讨论】:

感谢您的回复。酷我已经改变了代码,但它似乎仍然指向SendVerificationMail() return this.ngFireAuth.currentUser.sendEmailVerification() 我还添加了:aync SendVerificationMail() await this.ngFireAuth.currentUser.sendEmailVerification() 但仍然显示相同的错误 我已经更新了我的答案,您可以将 async/await 添加到组件中的调用者函数中,并保持服务原样 很奇怪,还是不行。因为 .then 在单独的函数中,所以它不会让 await 工作继续显示之前的错误加上 await 错误,因为它是单独的。 error TS1308: 'await' expression is only allowed within an async function. [ng] 23 await this.authService.SendVerificationMail() [ng] ~~~~~ [ng] src/app/shared/authentication-service.ts:45:40 - error TS2570: Property 'sendEmailVerification' does not exist on type 'Promise&lt;User&gt;'. Did you forget to use 'await'? [ng] 45 return this.ngFireAuth.currentUser.sendEmailVerification() 你在组件中的注册方法中添加了async吗?你必须在封闭函数中添加async关键字才能使用await 我做到了,这很奇怪,我使用了您在上面发布的内容,但仍然显示错误。我有两种类型的注册页面。现在使用相同的 signUp() 但它很快就会改变。但我不明白为什么会引起冲突?编辑:谢谢你帮我解决这个问题!【参考方案2】:

新版本 AngularFire 6.0.0 发布以支持 Angular 9。它不向后兼容与旧版本。它不再支持旧版本的 Angular (

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']);
    )
  

【讨论】:

【参考方案3】:

我有这个问题。

import  auth  from 'firebase/app';

因为 firebase/app 正在被移除。这取代了什么?我遇到了 googleAuth() 的问题。以前我也有一个问题,能够在 localhost 上使用弹出窗口登录,但在部署到 android 手机时出现错误。

 // 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(['tabs']);
    )
  this.SetUserData(result.user);
).catch((error) => 
  window.alert(error)
)

【讨论】:

这看起来不像答案。如果是解决方案,请描述它如何解决 OP 的问题【参考方案4】:

试试这个

// Send email verfificaiton when new user sign up   
async SendVerificationMail() 
(await this.afAuth.currentUser).sendEmailVerification()
.then(() => 
  this.router.navigate(['verify-email']);
)   

查看此注释https://github.com/angular/angularfire/issues/2409

【讨论】:

您好,请改进您的代码,如果您编写异步函数,请不要使用 .then 子句

以上是关于TS2570 错误协助:“Promise<User>”类型上不存在属性“sendEmailVerification”。您是不是忘记使用“等待”?的主要内容,如果未能解决你的问题,请参考以下文章

PromiseKit 6 中 firstly 的合成器

HDU-2570迷瘴(贪心)

Luogu P2570 [ZJOI2010]贪吃的老鼠

Luogu2570 ZJOI2010 贪吃的老鼠 二分答案+最大流

2570 大牛们请教,总是WA,思路跟HDU论坛上一样啊

协助处理 javascript 错误:TypeError: Cannot read property 'hash' of undefined