【问题标题】:Use SnapshotChanges with CombineLatest将 SnapshotChanges 与 CombineLatest 一起使用
【发布时间】:2018-11-18 18:18:58
【问题描述】:

我正在使用 combineLatest 合并来自 Firestore 的 3 个不同查询。但是,我不想使用 valueChanges(),我想使用 snapshotChanges()。

const newRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'new')).snapshotChanges().pipe(
  map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Application;
      const id = a.payload.doc.id;
      return {id, ...data};
    })
  })
);

const pendingRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'pending')).snapshotChanges().pipe(
  map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Application;
      const id = a.payload.doc.id;
      return {id, ...data};
    })
  })
);

const inprogressRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'in-progress')).snapshotChanges().pipe(
  map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Application;
      const id = a.payload.doc.id;
      return {id, ...data};
    })
  })
);

const result = combineLatest<any[]>(newRef, pendingRef, inprogressRef).pipe(
  map(arr => arr.reduce((acc, cur) => acc.concat(cur)))
);
return result;

如何合并这 3 个查询以获取它们各自的文档 ID? 我必须以这种方式编写 3 个查询还是有其他方式?我想简化代码。

【问题讨论】:

  • 你现在有什么问题?
  • 实际上一切正常,但我想以一种不必复制 snapshotChanges() 代码块的方式编写代码。

标签: javascript firebase rxjs google-cloud-firestore angularfire2


【解决方案1】:

很多方法可以减少代码。

一个非常简单的...

定义一个函数来完成这项工作:

function processChanges(changes) {
    return changes.map(a => {
      const data = a.payload.doc.data() as Application;
      const id = a.payload.doc.id;
      return {id, ...data};
    })
}

然后使用3次:

const newRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'new')).snapshotChanges().pipe(map(processChanges)));

const pendingRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'pending')).snapshotChanges().pipe(map(processChanges)));

const inprogressRef = this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', 'in-progress')).snapshotChanges().pipe(map(processChanges)));

const result = combineLatest<any[]>(newRef, pendingRef, inprogressRef).pipe(
  map(arr => arr.reduce((acc, cur) => acc.concat(cur)))
);
return result;

或者定义一个辅助函数并调用三次:

函数 getApplicationsForStatus(状态) {

return this.afs.collection('applications', ref => ref.orderBy('date_created_at', 'desc').where('status', '==', status)).snapshotChanges().pipe(
  map(changes => {
    return changes.map(a => {
      const data = a.payload.doc.data() as Application;
      const id = a.payload.doc.id;
      return {id, ...data};
    })
  })
);

并将其用作:

const newRef = getApplicationsForStatus('new');
const pendingRef = getApplicationsForStatus('pending'); 
const inprogressRef = getApplicationsForStatus('progress');

const result = combineLatest<any[]>(newRef, pendingRef, inprogressRef).pipe(
  map(arr => arr.reduce((acc, cur) => acc.concat(cur)))
);
return result;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多