I tried to get the document and the subcollection data at once in firestore.
And I used the async and await to deal with the forEach loop.
It still has some problem. The console.log 4
always executes first.
But what I expect the should be 1 -> 2 -> 3 -> 4.
Could anyone help me how to redesign my code?
let data = {};
toolboxesRef.get()
.then(async snapshot => {
let toolboxes = [];
// get toolbox
await snapshot.forEach(async doc => {
let toolbox = {};
await toolboxesRef.doc(doc.id).collection('tags').get()
.then(snapshot => {
let tags = []
snapshot.forEach(doc => {
tags.push(doc.id);
console.log(1)
})
toolbox.tags = tags;
toolbox.id = doc.id;
toolbox.data = doc.data();
console.log(2)
})
console.log(3)
toolboxes.push(toolbox)
})
console.log(4);
data.toolboxes = toolboxes
return data;
})