Я использую combLatest, чтобы объединить 3 разных запроса из Firestore. Однако я не хочу использовать 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 запроса, чтобы получить соответствующий идентификатор документа? Нужно ли мне так писать 3 запроса или есть другой способ? Я хочу упростить коды.
На самом деле все работает, но я хочу писать коды таким образом, чтобы мне не приходилось реплицировать блок кода snapshotChanges ().



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Есть подходы многие для сокращения кода.
Очень простой ...
Определите функцию для выполнения работы:
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;
В качестве альтернативы определите вспомогательную функцию и вызовите ее трижды:
function getApplicationsForStatus (status) {
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;
В чем проблема с тем, что у вас сейчас?