

How can I get all orders data from this database ?
This is how I have stored data :
db
.collection('users')
.doc(user?.email)
.collection('orders')
.doc(paymentIntent.id)
.set({
basket: basket,
amount: paymentIntent.amount,
created: paymentIntent.created
})
setSucceeded(true);
setError(null);
setProcessing(false);
You need to fetch initial collection snapshot and then get all subcollections.
If you're querying for a specific doc you can get a direct query result straight away:
const id = 123 // 'replace with whatever user you're trying to query'
const userOrders = await firebase.collection('users').doc(id)
.collection('orders').get()
.then(doc => doc.exists ? doc.data() : null)
Otherwise you'll need to perform two consequentive fetches
const ordersSnapshot = await firebase.collection('users').get()
const allOrders = await Promise.all(ordersSnapshot.docs.map(async ({ id }) => (
firebase.collection('users').doc(id).collection('orders').get()
.then(doc => doc.exists ? doc.data() : null)
)))
Using Collection Group Queries might be the easiest way which fetches all documents from collections with same name which is passed in collectionGroup() method:
db.collectionGroup("orders").get().then((querySnapshot) => {
console.log(querySnapshot.docs.map(d => ({id: d.id, ...d.data()})))
})
This will log an array of all orders.