简体   繁体   English

如何从 Android 上的 Firebase Firestore 获取嵌套集合?

[英]How to get nested collection from Firebase Firestore on Android?

I am working on an application where I have saved my data in firebase Firestore in nested collection now when I am trying to get/retrieve the data from Firestore but not able to get it.我正在开发一个应用程序,当我尝试从 Firestore 获取/检索数据但无法获取数据时,我将数据保存在嵌套集合中的 firebase Firestore 中。 please guide me where am I wrong??请指导我哪里错了??

CODE TO WRITE/ADD THE DATA IN FIRESTORE在 Firestore 中写入/添加数据的代码

  DocumentReference uidRef =  firebaseFirestore.collection("listing_details").document(uid);

    uidRef.collection("room_details").add(user).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
       @Override
       public void onSuccess(DocumentReference documentReference) {

           Toast.makeText(getContext(), "data added", Toast.LENGTH_SHORT).show();


       }
   })
    .addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {

            Toast.makeText(getContext(), "data adding failure", Toast.LENGTH_SHORT).show();
        }
    });

CODE FOR DATA RETRIEVING数据检索代码

  db.collection("listing_details").document().collection("room_details").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
            List<DocumentSnapshot> list = queryDocumentSnapshots.getDocuments();
            for (DocumentSnapshot d : list)
            {
                RoomsDetails obj = d.toObject(RoomsDetails.class);
                roomsDetails.add(obj);
            }
            roomsAdapter.notifyDataSetChanged();

        }
    });

DATA RETRIEVING CODE (UPDATED)数据检索代码(更新)

  roomDetailsRef.document(doc_id).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {

        //GUIDE ME HERE HOW CAN I ITERATE THROUGH IT SIR PLEASE
            
        }
    });

Each time you're calling .document() to create the following reference, without passing anything as an argument:每次调用.document()来创建以下引用时,不传递任何内容作为参数:

db.collection("listing_details").document().collection("room_details")
//                                👆

It means that you're generating a brand new unique document ID.这意味着您正在生成一个全新的唯一文档 ID。 If you want to create a reference that points to a particular document, then you have to pass the particular document ID that already exists in the database to the document() method, and not generate a new one.如果要创建指向特定文档的引用,则必须将数据库中已存在的特定文档 ID 传递给document()方法,而不是生成新的。

So your code should look like this:所以你的代码应该是这样的:

//Code to add data to Firestore.
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference uidRef =  db.collection("listing_details").document(uid);
CollectionReference roomDetailsRef = uidRef.collection("room_details");
String docId = roomDetailsRef.document().getId();
roomDetailsRef.document(docId).set(user).addOnSuccessListener(/*.../*);
//                       👆

See, I have used DocumentReference#getId() to get the ID of the document, and DocumentReference#set(Object data) to actually add the document to Firestore.看,我使用DocumentReference#getId()来获取文档的 ID,并使用DocumentReference#set(Object data)将文档实际添加到 Firestore。

//Code to read data from Firestore.
roomDetailsRef.document(docId).get().addOnSuccessListener(/*.../*);
//                       👆

See, I have passed the document ID that was generated earlier, to the CollectionReference#document() method.看,我已将之前生成的文档 ID 传递给CollectionReference#document()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM