将 Angularfire2/Firestore 可观察转换为承诺



>你能告诉我如何将Angularfire2/Firestore observable转换为promise吗?我已经尝试过如下所示。但它不起作用,也没有错误。

.ts

  this.categories$ = this.categoryProvider.getAllContactCategories().valueChanges();
   const defaultCategories = await this.categories$.map((response) => response).toPromise();//after this line it won't execute
   await Promise.all(defaultCategories.map(async (c: Category) => {
   await this.categoryProvider.createCategory(c.name, project.id);
      }));

提供者.ts

  getAllContactCategories(): AngularFirestoreCollection<Category> {
    return this.fireStore.collection(`contactCategories`);
  }

我是这样做的:干杯:)

注意: 这只是为了更新master table的几个默认记录。仅此而已:)

import { first } from 'rxjs/operators';
this.categories$ = this.categoryProvider.getAllContactCategories().valueChanges();
const defaultCategories = await this.categories$.map(categories => {
     categories.forEach(async (c) => {
         await this.categoryProvider.createCategory(c.name, project.id);
      });
   }).pipe(first()).toPromise();

最新更新