Ваш Promise.all () перехватывает обещания получения только подресурсов и комментариев, а не самих «Ресурсов». поэтому для любого ресурса, который может завершить выборку сначала подресурса и комментариев, он разрешает и возвращает данные.
Ниже приведено небольшое изменение вашего кода, так что он ожидает, пока все ресурсы завершат извлечение комментариев / подресурсов и затем возвращает их в виде массива.
response.set('Cache-Control','public, max-age=300, s-maxage=600');
db.collection('Resources').get()
.then(snapshot => {
let ResourcePromises = snapshot.docs.map(doc => {
var promises = [];
let documentData = doc.data();
documentData['id'] = doc.id;
promises.push(db.collection('Resources').doc(documentData.id).collection('SubResources').get()
.then(snapshot => {
documentData['subresources'] = snapshot.docs.map(doc => {
let subResourceData = doc.data();
subResourceData['id'] = doc.id;
return subResourceData;
})
}));
promises.push(db.collection('Resources').doc(documentData.id).collection('Comments').get()
.then(snapshot => {
documentData['comments'] = snapshot.docs.map(doc => {
return doc.data();
})
}));
return Promise.all(promises).then(function(){
//console.log(documentData);
//return response.json(documentData);
return documentData;
})
});
Promise.all(ResourcePromises).then(function(allResources) {
return response.json(allResources);
})
})
.catch(err => {
console.log('Error getting documents', err);
});
Вы можете реорганизовать код для разделения функций, чтобы они были более читабельными.