将Angularfire2 / Firestore observable转换为promise

时间:2018-03-14 11:55:01

标签: angular typescript promise observable google-cloud-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);
      }));

provider.ts

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

1 个答案:

答案 0 :(得分:-1)

我这样做了:干杯:)

注意: 这仅用于更新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();