8
votes

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
  • products

    • product_id
      • product_desc
      • price

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;
  }
1
There are no server-side joins in Cloud Firestore. If you need to read documents from two different collections, that requires two separate reads.Frank van Puffelen
thanks for replying Frank van Puffelen my comment was to long so i had posted a reply to the my question.JWRich
Instead of posting it as an answer, just edit it into your question. There's an edit link right under it.Frank van Puffelen
i think i have soled it i will post the answer in the question when i have confirmed it but i think it was just a misunderstanding of how you handle a future. again thanks.JWRich
OK, that's good to hear. But now you've added the answer to your question. Please take a moment to learn the rules of Stack Overflow, which (unlike most forums you might be familiar with) uses a specific Q&A format. Instead of editing your answer into your original question, you should add it as an answer. That way others (and the system) can easily see that there's an answer, and you might gain some reputation from upvotes. But if (like earlier) you have additional information for your question, edit that into your question via the link under it.Frank van Puffelen

1 Answers

5
votes

Short answer i didn't pay attention to how you use a future

Get the productId from the user table

  Future<List<DocumentSnapshot>> getProduceID() async{
    var data = await Firestore.instance.collection('users').document(widget.userId).collection('Products').getDocuments();
    var productId = data.documents;
    return productId;
  }

Then call this using .then() rather than try to apply the returned data to the variable as that is not how a future it works

  var products;
  getProduceID().then((data){
  for(int i = 0; i < s.length; i++) {
    products = Firestore.instance.collection('products')
        .document(data[i]['productID'])
        .snapshots();
    if (products != null) {
      products.forEach((product) {
        print(product.data.values);
      });
    }
  }
});