我有一个异步方法如下。它在这里 await userProfile.set({
显示错误[ts] 'await' expression is only allowed within an async function.
。你能告诉我该怎么解决吗?
注意:也许是因为我尝试在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()
});
}
});
}
observable/subscribe
中调用promise
? - Sampathfirestore
问题引起的。https://groups.google.com/forum/#!topic/google-cloud-firestore-discuss/hjng5VgktPM - Sampath