Problem:
Attached is my db structure. How can I get a specific saleOrder item by its id?
Below is my query but it isn’t working.
Thank you in advance.
const ref = collection(db, 'customers', customerId, 'saleOrders');
const q = query(ref, where('id', '==', saleOrderId));
const querySnapshot = await getDocs(q);
Solution:
You’re querying
collection(db, 'customers', customerId, 'saleOrders');
So this is:
- A collection
customers
, which you have - A document in there for the
customerId
, which I assume you have - A *subcollection under there called
saleOrders
, which you don’t have
You cannot query a nested map field with a query operator like that. Even if you could, it wouldn’t accomplish anything – as Firestore client-side SDKs always return the full document, not a subset of its fields.
You have a few options:
-
Most logically would be to create an actual
saleOrders
subcollection, store theID
in there as a top-level field, and then query that subcollection as you now do. -
Alternatively you can load the full document you have right now, and filter the sale orders in your client-side code.