Firestore Getting documents id from collection

后端 未结 9 1180
陌清茗
陌清茗 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: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 = ,
      field2 = ,
      ..
      id = 'whatEverTheDocumentIdWas'
    }
    

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

    AngularFire 5.2.0

提交回复
热议问题