56

I have an async method like below. It shows an error [ts] 'await' expression is only allowed within an async function. here await userProfile.set({. Can you tell me how to sort out it?

Note: Maybe it is due to I have tried to call promise inside the observable. Any clue?

 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()
        });
      }
    });
  }

1 Answer 1

92

Your main function is async but you’re using await in an arrow function which isn’t declared as async

userProfiles$.subscribe(async res => {
  if (res == null) {
    await userProfile.set({
...
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, That is fixed. But am I doing right way here? calling promise inside the observable/subscribe?
Above leads to another issue.Please see this: stackoverflow.com/questions/47710673/…
Thanks for the help. Above issue maybe due to firestore issue. groups.google.com/forum/#!topic/google-cloud-firestore-discuss/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.