Firestore会自动订阅

问题描述 投票:1回答:1

临时解决方案

我在这里做了非常丑陋的黑客攻击。你知道更好的解决方案吗?

const sub = this.budgetGroups$.subscribe(res => {
      if (res.length > 0) {
        this.showToast.showErrorToast('Budget Group already exists');
        sub.unsubscribe();
        return false;
      } else {
        this.budgetGroupProvider.createTempBudgetGroup(name);
        this.isBudgetGroupAvailable = true;
        sub.unsubscribe();
      }
    },
      err => { console.log(err); sub.unsubscribe() },
      () => sub.unsubscribe()
    );

原始问题:

我正在使用我的应用程序的firestore。我在下面的实现中看到了一个非常关键的问题。我创建预算组时工作正常。之后,我使用firebase控制台删除了该预算组。那时这种方法一次又一次地发射。你能告诉我怎么办吗?希望我在这里错误地使用subscribe

page.ts

 createBudgetGroup(name: string, projectId?: string) {
         this.budgetGroups$ = this.budgetGroupProvider.getSpecificTempBudgetGroup(name).valueChanges();
         this.budgetGroups$.subscribe(res => {//here is the issue
          if (res.length > 0) {
            this.showToast.showErrorToast('Budget Group already exists');
            return false;
          } else {
            this.budgetGroupProvider.createTempBudgetGroup(name);
            this.isBudgetGroupAvailable = true;
          }
        },
          err => console.log(err)
        );
      }

provider.ts

  getSpecificTempBudgetGroup(name: string): AngularFirestoreCollection<BudgetGroup> {
    return this.fireStore.collection<BudgetGroup>(`members/${this.authenticationProvider.member.id}/budgetGroups`, ref => ref
      .where('name', '==', name)
    );
  }
angular firebase rxjs ionic3 google-cloud-firestore
1个回答
1
投票

Firebase observable永远不会完成,因为它是一个实时数据库。

您可以使用first运算符来克服它

rxjs ^ 5.5

import { first } from 'rxjs/operators';

this.budgetGroups$.pipe(first()).subscribe

对于早期的rxjs版本

import 'rxjs/add/operator/first';

this.budgetGroups$.first().subscribe
© www.soinside.com 2019 - 2024. All rights reserved.