Im bulding a shopping cart with Firebase, when a user have paid a function triggers and I get all the items from the cart then I create and array of products Id's (pids) after that, I want to search the store ID of each item to notify them.
The store ID is a child in the product node. Products->{pid}->sid
This is how I do after getting the pids
.then(snap => {
return pids.forEach( pids => {
productIdRef = root.child(`/products/${pids}`);
//Here I should create a promise to get the Stores Ids:
// productIdRef.once("value")....
});
That is nesting promises and as far as I know is not good.
So is there something I could do to avoid nesting promises or is this doomed from the beginning due to a bad Firebase database design?
Update: complete function:
var itemsRef = NOTIFICATION_SNAPSHOT.adminRef.parent.child('items');
var pids = [];
return itemsRef.once('value').then(snapshot => {
return snapshot.forEach((childSnapshot) => {
pids.push(childSnapshot.key);
});
}).then(snap => {
return pids.forEach(pids => {
productIdRef= root.child(`/products/${pids}`).once('value');
});
}).catch(error => {
// Something went wrong.
console.error(error);
});
I'm not sure what you mean by nested
Perhaps use Promise.all
like below:
.then(snap => {
return Promise.all(pids.map(pid => {
return root.child(`/products/${pid}`);
}));
}).then((storeIds) => {
// result of root.child()
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.