【问题标题】:'await' expression is only allowed within an async function'await' 表达式只允许在异步函数中使用
【发布时间】:2018-05-22 10:34:10
【问题描述】:

我有一个如下所示的异步方法。它在此处显示错误[ts] 'await' expression is only allowed within an async function. await userProfile.set({。能告诉我怎么解决吗?

注意:可能是因为我尝试在observable 中调用promise。有什么线索吗?

 async loginWithGoogle(): Promise<void> {
    const result = await this.afAuth.auth.signInWithPopup(new firebase.auth.GoogleAuthProvider());
    const userId: string = result.uid;
    const userProfile: AngularFirestoreDocument<UserProfile> = this.fireStore.doc(`userProfile/${userId}`);
    const userProfiles: AngularFirestoreCollection<UserProfile> = this.fireStore.collection('userProfile/', ref => ref.where('email', '==', result.email));
    const userProfiles$: Observable<UserProfile[]> = userProfiles.valueChanges();
    userProfiles$.subscribe(res => {
      if (res == null) {
        await userProfile.set({ //here it shows error
          id: userId,
          email: result.email,
          creationTime: moment().format(),
          lastSignInTime: moment().format()
        });
      }
    });
  }

【问题讨论】:

    标签: angular rxjs ionic3 angularfire2 google-cloud-firestore


    【解决方案1】:

    您的主函数是异步的,但您在未声明为 async 的箭头函数中使用了 await

    userProfiles$.subscribe(async res => {
      if (res == null) {
        await userProfile.set({
    ...
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 2016-02-08
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多