I am playing with flutter but I ran into an issue with firestore that I can't figure out.
let's say I would like to retrieve the purchaser history of a customer and I have a firestore as described below so I have a collection of "user" within that contains a document of user_id
's and then within that, I have a "products" collection with a document that contains the product_id
's my aim is to gather the product id's for the selected user and then retrieve the products price from the products collection?
users
- user_id_1
- products
- purchace_id
- product_id
- quantity
- purchace_id
- products
- user_id_1
products
- product_id
- product_desc
- price
- product_id
the issue I am having is while I can get the id for the products from the user table but I can't then see a simple way of how I can use this data like with SQL where we would make a simple join on the product_id to get the price where the id's match?
any help would be much appreciated.
Thanks for replying Frank van Puffelen 2 seprate querys seams reasonable but it brings up other issues with the second query as now with the code below i can get the documents_id from the user and then do a query on the products and everything looks ok so i think i am going in the correct direction as i can print the id of the document in the loop to ensure i am accessing the correct ones but when i change this to get a snapshot of the document i cant access the data within this is probably me doing something silly or misunderstanding something but this feels very awkward compared to sql when trying to get and work with the data. For example if i return the data it wants it to be in the form of a future> however once i return the data in that format then i can no longer access the id's in the same way as i was doing.
Future<List<DocumentSnapshot>> getSeedID() async{
var data = await Firestore.instance.collection('users').document(widget.userId).collection('products').getDocuments();
var productList = data.documents;
print("Data length: ${productList.length}");
for(int i = 0; i < productList.length; i++){
var productId = Firestore.instance.collection('products').document(productList[i]['productId']).documentID;
if(productId != null) {
print("Data: " + productId);
}
}
return productList;
}