【问题标题】:Firestore Array of Document ReferencesFirestore 文档引用数组
【发布时间】:2018-03-26 09:36:53
【问题描述】:

我的 Firestore 数据库具有以下结构:

products:{ // Collection
    procuct_1: { // Document
       title:"",
       url:""

videos:{ // Collection
    video_1:{ // Document
       title:"",
       products:{ // Array of references 
            0: "/products/product_1,
            1: "/products/product_2

我希望能够从 Videos Collection 中的 Array 字段获取对 Products 集合 的文档引用,以便获取此集合的某些字段的值(例如。产品名称)。

目前我使用此代码:

    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    firebaseFirestore
            .collection("videos")
            .get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot documentSnapshot = task.getResult();

                        Map<String, Object> map = documentSnapshot.getData();
                        for (Map.Entry<String, Object> entry: map.entrySet()){
                            if (entry.getKey().equals("products")){
                               textView.setText(entry.getValue().toString());
                            }
                        }

但是entry.getValue().toString(),返回给我这样的数组:

[com.google.firebase.firestore.DocumentReference@451d5ae8,

com.google.firebase.firestore.DocumentReference@e28cd0c]

有什么建议我可以从这个数组中获取产品集合的任何字段吗?

【问题讨论】:

    标签: java android firebase google-cloud-firestore


    【解决方案1】:

    要解决这个问题,请使用以下代码:

    firebaseFirestore.collection("videos")
        .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                for (DocumentSnapshot document : task.getResult()) {
                    List<String> products = (List<String>) document.get("products");
                    for (String product : products) {
                        Log.d("TAG", product);
                    }
                }
            }
        });
    

    输出将是:

    /products/product_1
    /products/product_2
    

    【讨论】:

    • 感谢您的回答。但我收到错误 java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Map 在这行代码Map&lt;String, Object&gt; products = (Map&lt;String, Object&gt;) document.get("products");
    • 对不起,我的回答迟了。我没有在您的数据库结构中看到括号,所以我认为它是Map,这就是该错误的原因。请参阅我更新的答案。现在可以肯定了。
    【解决方案2】:

    这段代码让我得到了我需要的结果

    FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
        firebaseFirestore
                .collection("videos")
                .get()
                .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                        DocumentSnapshot document = task.getResult();
                        ArrayList<DocumentReference> products = (ArrayList<DocumentReference>) document.get("products");
                        for (int i = 0; i < products.size(); i++) {
                            DocumentReference ref = products.get(i);
                            ref.get()
                                    .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                                        @Override
                                        public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                                            if (task.isSuccessful()) {
                                                DocumentSnapshot document = task.getResult();
                                                if (document != null && document.exists()) {
                                                    BestBuysProducts best_buy = document.toObject(BestBuysProducts.class);
                                                    Log.d(TAG, "Title: " + best_buy.getProduct_title());
                                                }
                                            }
                                        }
                                    });
                        }
                    }
                });
    

    【讨论】:

    • 我看到这行得通,但您所做的并不是一个好习惯,因为您为该循环的每次迭代都附加了一个侦听器。因此,为不需要保持同步的数据保持监听器是一种浪费。请仅使用几行代码查看我的更新答案。
    • 很高兴听到它有效!如果您认为我的回答对您有所帮助,请考虑采纳。谢谢!
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 2019-01-09
    • 2019-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    相关资源
    最近更新 更多