How to get document id from firestore database

会有一股神秘感。 提交于 2020-06-17 15:11:38

问题


I'm trying to retrieve a document id by calling doc.getId() but I can't call it from this code

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()) {
                        if (doc.getType() == DocumentChange.Type.ADDED) {
                            Pegawai pegawai = doc.getDocument().toObject(Pegawai.class);
                            pegawai.setId(doc.getId());
                            pegawaiList.add(pegawai);

                            pegawaiListAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });

I've tried this code, and apparently, I can call doc.getId() with this code, but this code isn't populating my recyclerview at all

        db.collection("Pegawai").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e != null) {
                    Log.d(TAG, e.toString());
                }
                if (queryDocumentSnapshots != null) {
                    for (DocumentSnapshot doc : queryDocumentSnapshots) {
                        Pegawai pegawai = doc.toObject(Pegawai.class);
                        pegawai.setId(doc.getId());
                        pegawaiList.add(pegawai);
                    }
                }
            }
        });

回答1:


A DocumentChange object has a method getDocument() which returns a QueryDocumentSnapshot object. This is a subclass of DocumentSnapshot which means it also has a getId() method. So you should be able to use doc.getDocument().getId() to get the ID of the document being processed.




回答2:


The following is how to solve it in Javascript, maybe this will help you to convert it to Java. Using the following method, we can retrieve the document id, dynamically. Notice that we use snapshotChanges(). This is important because once we subscribe to this function, contained in the subscription is the document id.

saveMyValuesHere: any[] = []

getPegawai() {

    return this.db.collection('Pegawai').snapshotChanges();

  }

 getPegawai().subscribe(serverItems => {
      this.saveMyValuesHere = [];
      serverItems.forEach(a => {
        const data = a.payload.doc.data();
        const id = a.payload.doc.id;
        this.saveMyValuesHere.push({ id, ...data});
      });
    });



来源:https://stackoverflow.com/questions/57068280/how-to-get-document-id-from-firestore-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!