我有一个像下面这样的异步方法。它在[ts] 'await' expression is only allowed within an async function.
显示错误await userProfile.set({
。你能告诉我如何理清吗?
注意:也许是因为我试图在promise
内拨打observable
。任何线索?
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()
});
}
});
}
答案 0 :(得分:9)
您的主要功能是异步,但您在箭头函数中使用await
,该函数未声明为async
userProfiles$.subscribe(async res => {
if (res == null) {
await userProfile.set({
...