1

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.

Eshan
  • 115
  • 7

2 Answers2

0

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!

Frosty619
  • 1,381
  • 4
  • 23
  • 33
0

See the example on this documentation on getting all documents in a collection:

db.collection("cities").whereField("capital", isEqualTo: true)
    .getDocuments() { (querySnapshot, err) in
        if let err = err {
            print("Error getting documents: \(err)")
        } else {
            for document in querySnapshot!.documents {
                print("\(document.documentID) => \(document.data())")
            }
        }
}

You're looking for the documentID member of the DocumentSnapshot objects.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807