How to include the document id in Firestore collection in Angular 5

后端 未结 6 1087
温柔的废话
温柔的废话 2021-01-02 11:28

I have this function that gets all the posts and I want to get the id of the post any idea how to get the id of the post or How to include the id when I add the document to

6条回答
  •  半阙折子戏
    2021-01-02 11:41

    In my case, because I only want to get the data without subscribing to it, I'm adding the id in the then:

    **If you want the data in a different object: **

    await this.$fireStore
        .collection('SOME_COLLECTION')
        .doc('SOME_ID')
        .get()
        .then((item) => ({ id: item.id, data: item.data() }))
    

    **If you want the data spread in the returned object: **

    await this.$fireStore
        .collection('SOME_COLLECTION')
        .doc('SOME_ID')
        .get()
        .then((item) => ({ id: item.id, ...item.data() }))
    

提交回复
热议问题