I have below data structure that will be added to the firestore collection
as document
{
".......": "......." //few more properties
"productInfo": {
"0": {
"code": "SC05",
"price": 400,
"productId": "asUsd1HPEPOo2itiKxdash",
".......": "......." //few more properties
"addOns": [{
"aid": "4casedasgdfdgfas",
"price": "50",
"......": "....." //few more properties
}]
}
}
}
So I will be having array of products object and each product will have array of addon object.
I am trying to retrieve price from db before saving it as document and hence I've written .onCreate
function for that particular table. To retrieve price on the product
level and addon
level, I am having 2 promises as below:
const productPromises = [];
_.map(products, (product) => {
productPromises.push(new Promise((resolve, reject) => {
db.doc(`product/${product.productId}`).get().then(docData => {
return resolve({
productId: product.productId,
halfkgprice: docData.data().halfkgprice,
price: docData.data().price
});
}).catch(reason => {
return reject(reason);
});
}));
});
const addonPromise = [];
_.map(products, (product) => {
addonPromise.push(new Promise((resolve, reject) => {
if (product.addOns !== undefined && product.addOns !== null) {
return _.map(product.addOns, (addon) => {
return db.doc(`addons/${addon.aid}`).get().then((addonData) => {
return resolve({
price: addonData.data().price
});
}).catch(reason => {
return reject(reason);
})
});
} else {
return resolve();
}
}));
});
and then I do have
Promise.all([productPromises, addonPromise]).then(result => {
//do something with result
});
But this will not wait any of the promise to resolve. When I log the result I get as below.
console.log(result) will show [ [ Promise { <pending> } ], [ Promise { <pending> } ] ] and doesn't wait for them to resolve
My question is, why is not waiting for all the promises to resolve? Is there any problem with the way I am returning data from firestore
get query
?