扑查询firestore中的多个集合 [英] flutter query multiple collections in firestore

查看:45
本文介绍了扑查询firestore中的多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩弄颤抖,但是遇到了我无法弄清的Firestore问题.

I am playing with flutter but I ran into an issue with firestore that I can't figure out.

假设我想检索客户的购买者历史记录,并且拥有一个如下所述的存储区,因此我有一个用户"集合,其中包含一个user_id的文档,然后在其中有一个包含产品product_id的文档的产品"集合,我的目的是为所选用户收集产品ID,然后从产品集合中检索产品价格?

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
    • 产品
      • purchace_id
        • product_id
        • 数量
        • user_id_1
          • products
            • purchace_id
              • product_id
              • quantity

              产品

              • product_id
                • product_desc
                • 价格
                • product_id
                  • product_desc
                  • price

                  我遇到的问题是,我可以从用户表中获取产品的ID时,却看不到如何像SQL一样使用这种数据的简单方法,我们可以在其中进行简单的连接product_id以获取与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?

                  任何帮助将不胜感激.

                  感谢Frank van Puffelen 2的单独查询,虽然是合理的,但第二个查询带来了其他问题,因为现在使用下面的代码,我可以从用户那里获取documents_id,然后对产品进行查询,一切看起来都很好所以我认为我朝着正确的方向前进,因为我可以在循环中打印文档的ID,以确保我访问的是正确的ID,但是当我更改它以获取文档的快照时,我无法访问其中的数据可能我在做一些愚蠢的事情或误解了一些事情,但是当尝试获取和使用数据时,与sql相比,这感觉很尴尬.例如,如果我返回数据,希望它以Future的形式出现>,但是一旦我以该格式返回数据,则我将无法再以与以前相同的方式访问ID.

                  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;
                    }
                  

                  推荐答案

                  简短的回答,我没有注意您如何使用未来

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

                  从用户表中获取productId

                  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()调用此方法,而不是尝试将返回的数据应用于变量,因为这不是将来的工作原理

                  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);
                        });
                      }
                    }
                  });
                  

                  这篇关于扑查询firestore中的多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆