Firestore Getting documents id from collection

后端 未结 9 1142
陌清茗
陌清茗 2020-11-30 00:16

I\'m trying to retrieve my documents with id but can\'t figure it out.
Currently I retrieve my documents like this :

const racesCollection: AngularFir         


        
相关标签:
9条回答
  • 2020-11-30 00:47

    For document references, not collections, you need:

    // when you know the 'id'
    
    this.afs.doc(`items/${id}`)
      .snapshotChanges().pipe(
        map((doc: any) => {
          const data = doc.payload.data();
          const id = doc.payload.id;
          return { id, ...data };
        });
    

    as .valueChanges({ idField: 'id'}); will not work here. I assume it was not implemented since generally you search for a document by the id...

    0 讨论(0)
  • 2020-11-30 00:49

    Can get ID before add documents in database:

    var idBefore =  this.afs.createId();
    console.log(idBefore);
    
    0 讨论(0)
  • 2020-11-30 00:56

    For angular6+

        this.shirtCollection = afs.collection<Shirt>('shirts');
        this.shirts = this.shirtCollection.snapshotChanges().pipe(
            map(actions => {
            return actions.map(a => {
                const data = a.payload.doc.data() as Shirt;
                const id = a.payload.doc.id;
                return { id, ...data };
            });
            })
        );
    
    0 讨论(0)
  • 2020-11-30 00:56

    Since you are using angularFire, it doesn't make any sense if you are going back to default firebase methods for your implementation. AngularFire itself has the proper mechanisms implemented. Just have to use it.

    valueChanges() method of angularFire provides an overload for getting the ID of each document of the collection by simply adding a object as a parameter to the method.

    valueChanges({ idField: 'id' })
    

    Here 'idField' must be same as it is. 'id' can be anything that you want your document IDs to be called.

    Then the each document object on the returned array will look like this.

    {
      field1 = <field1 value>,
      field2 = <field2 value>,
      ..
      id = 'whatEverTheDocumentIdWas'
    }
    

    Then you can easily get the document ID by referencing to the field that you named.

    AngularFire 5.2.0

    0 讨论(0)
  • 2020-11-30 01:05

    I have tried this

    return this.db.collection('items').snapshotChanges().pipe(
              map(actions => {       
                return actions.map(a => {
                  const data = a.payload.doc.data() as Item;
                  data.id = a.payload.doc.id;
                  data.$key = a.payload.doc.id;
                  return data;
                });
              })
            );
    
    0 讨论(0)
  • 2020-11-30 01:08

    For angular 8 and Firebase 6 you can use the option id field

          getAllDocs() {
               const ref = this.db.collection('items');
               return ref.valueChanges({idField: 'customIdName'});
          }
    

    this adds the Id of the document on the object with a specified key (customIdName)

    0 讨论(0)
提交回复
热议问题