【问题标题】:forEach() fire first until it's finished after that, firebase worksforEach() 首先触发,直到完成之后,firebase 才能工作
【发布时间】:2018-04-06 19:14:03
【问题描述】:

我正在使用棱角分明、棱角分明的材料和 Firebase。 我在调试时发现了这个问题。

预期

我所期望的是 forEach 的项目进入searchCategory 方法并执行this.categorySubscription = this.storeService.doc$(path).subscribe();

但是,现在在我的代码中,forEach 一直有效,直到完成之后,firestore 订阅才会执行。

所以,它是一个组件,当用户添加一个类别时,如果服务器中没有类别,则区分一个新的类别。否则,它将用户添加的类别区分为已存在。

过程

过程是,首先,将类别放入类别列表并与 forEach 一起使用。当第一个类别进入 searchCategory 方法并使用 firestore 查询在服务器上搜索该类别时。 在订阅中,我们可以得到一个数据,如果它是空的——意味着服务器上没有类别——或者不是。因此,如果数据为空,那么我将使用添加查询,否则,使用更新查询。

如果您提供任何建议或提示,可能会很有帮助。谢谢。

这是我的代码

this.storeInfo.value['categoryChips'].forEach(category => {
      this.searchCategory(category);
}

//...

searchCategory(category) {
   this.categorySubscription = this.storeService.doc$(`categories/${category.name}`).subscribe(
  data => {
    if (data === null || data === undefined) {
      // add a new category

      this.addCategory(category);
      this.getRelStore(category.name);
    }
      else {
      // update          
      // this.test2(category, path, data['id']);
      // this.categorySubscription.unsubscribe();
      // this.categoryNum++;
      // this.getRelStore(category.name);
    }
  }
);
}

//...
//get related stores as an array
  getRelStore(name) {
    this.categorySubscription.unsubscribe();
    this.loadRel = this.storeService.doc$(`categories/${name}/relatedStore/store`).subscribe(
  data => {

    // there's no related store.
    if (data !== null && data !== undefined) {
      let tempRelatedStores;
      tempRelatedStores = data['name'];
      tempRelatedStores.push(this.storeName);
      this.addRelStore(name, tempRelatedStores);
    } else { // there are related stores already
      this.storeService.set(`categories/${name}/relatedStore/store`, {
        name: this.storeName
      });
    }
  });

【问题讨论】:

    标签: angular typescript firebase google-cloud-firestore


    【解决方案1】:

    我用.take(1) 解决了。 我在subscribe() 前面添加.take(1)。 它发送了我所期望的一件物品。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      • 1970-01-01
      • 2021-03-20
      • 1970-01-01
      • 1970-01-01
      • 2015-09-18
      • 2020-06-23
      相关资源
      最近更新 更多