Skip to content Skip to sidebar Skip to footer

How To Get Fields In A Firestore Document?

I am working on some Cloud functions that works with Firestore. I am trying to get a list of fields of a specific document. For example, I have a document reference from the even.d

Solution 1:

As the documentation on using Cloud Firestore triggers for Cloud Functions shows, you get the data of the document with event.data.data().

Then you can iterate over the field names with JavaScript's Object.keys() method or test if the data has a field with a simple array check:

exports.tryHasChild=functions.firestore.document('cities/{newCityId}')
.onWrite((event) =>{
  if (event.data.exists) {
    let data = event.data.data();
    Object.keys(data).forEach((name) => {
      console.log(name, data[name]);
    });
    if (data["population"]) {
      console.log("The edited document has a field of population");
    }
});

Post a Comment for "How To Get Fields In A Firestore Document?"