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

后端 未结 6 1102
温柔的废话
温柔的废话 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:35

    You could use the js spread operator and put that inside the query snapshot so using a bit of your code...

        async get_the_posts(){
            this.Post_collection = this.afs.collection('posts'); 
            return this.Posts = await this.Post_collection.onSnapshot(querySnapshot =>{
                            let arr = []
                            querySnapshot.forEach(function (doc) {
                                arr.push({id:doc.id, ...doc.data()});
    
                            });
                            return arr
            })
        }
    

提交回复
热议问题