Search code examples
swiftfirebasegoogle-cloud-firestoreuid

How to get UID of document in Cloud Firestore Swift


Currently, my app is looping through all the documents in a collection in firebase, however I need to know which UID the data is from so that I can add to it later. Is there a way to find this out and if not, what are other alternative approaches that I can go about to accomplish this. Thank you so much for your help.


Solution

  • Here is an example of how to get posts with id:

    let docQuery = this._db.collection('communityProgramPost')
    
    
    const response = await docQuery.get();
    
    const posts = response.docs.map(doc => {
      const post = doc.data();
      post.id = doc.id;
      return post;
    });
    

    Note that you're explicitly creating an id property on post and assigning it the value from doc.id. Simply returning doc.data() will not get you the id!