I would like my callback function to wait until ClientProductLinks
has been filled before sending it via res.send(). I'm sure there's a simple solution but I'm new to JS and cannot seem to figure it out.
app.all('/:client/listproducts', async function(req, res) {
const client = req.params.client;
let clientProductLinks = [];
const clientRef = await db.collection(client).get();
const snapshot1 = await clientRef;
snapshot1.forEach(async function (prod) {
let prodRef = await db.collection(client).doc(prod.id).collection(prod.id).get();
let snapshot2 = await prodRef;
snapshot2.forEach(function (doc) {
const obj = {
docID: doc.id,
docData: doc.data()
};
clientProductLinks.push(obj);
});
});
res.send(clientProductLinks);
});
Currently, res.send() sends ClientProductLinks
back as an empty array. Any help is greatly appreciated.