在 valueChanges 订阅中捕获 Firestore 错误并在失败时重试

Tim*_*Tim 0 javascript firebase typescript angular google-cloud-firestore

我使用 Firestore 来存储我的 Angular 应用程序的数据。我创建了一个服务并读取数据,例如:

retrieveCollectionColors(name) {
  this.db.collectionGroup('collection-colors', ref => ref.where('product', '==', name))
    .valueChanges().subscribe( (val: []) => {
    this.collectionColors.next(val);
  });
}
Run Code Online (Sandbox Code Playgroud)

如何捕获错误,如果发生错误,如何重试查询?

Pet*_*dad 7

subscribe方法包含第二个参数以防出错:

Rx.Observable.prototype.subscribe([observer] | [onNext], [onError], [onCompleted])

  • [观察者](Observer):接收通知的对象。

  • [onNext] (Function):为可观察序列中的每个元素调用的函数。

  • [onError] (Function):在可观察序列异常终止时调用的函数。
  • [onCompleted] (Function):在可观察序列正常终止时调用的函数。

因此,请执行以下操作:

.valueChanges().subscribe( (val: []) => {
    this.collectionColors.next(val);
  },error => {
  console.log(error);
  });
Run Code Online (Sandbox Code Playgroud)