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.data.ref
, but I am not sure if the document contains the field I am looking at. I want to get a list of the fields' name, but I am not sure how to do it.
I was trying to use Object.keys()
method to get a list of keys of the data, but I only get a list of number (0, 1...), instead of name of fields.
I tried to use the documentSnapShot.contains()
method, but it seems doesn't work.
exports.tryHasChild=functions.firestore.document('cities/{newCityId}')
.onWrite((event) =>{
if (event.data.exists) {
let myRef = event.data.ref;
myRef.get().then(docSnapShot => {
if (docSnapShot.contains('population')) {
console.log("The edited document has a field of population");
}
});
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.data.ref
, but I am not sure if the document contains the field I am looking at. I want to get a list of the fields' name, but I am not sure how to do it.
I was trying to use Object.keys()
method to get a list of keys of the data, but I only get a list of number (0, 1...), instead of name of fields.
I tried to use the documentSnapShot.contains()
method, but it seems doesn't work.
exports.tryHasChild=functions.firestore.document('cities/{newCityId}')
.onWrite((event) =>{
if (event.data.exists) {
let myRef = event.data.ref;
myRef.get().then(docSnapShot => {
if (docSnapShot.contains('population')) {
console.log("The edited document has a field of population");
}
});
Share
Improve this question
edited Jan 22, 2018 at 0:27
Frank van Puffelen
601k85 gold badges890 silver badges860 bronze badges
asked Jan 21, 2018 at 22:44
Fred XieFred Xie
452 silver badges5 bronze badges
3
- post your code.. that helps us to help you with providing a better answer. Wele to SO and finish the tour ;-) – ZF007 Commented Jan 21, 2018 at 22:52
- For example: exports.tryHasChild = functions.firestore.document('cities/{newCityId}').onWrite((event) =>{ if (event.data.exists) { let myRef = event.data.ref; myRef.get().then(docSnapShot => { if (docSnapShot.contains('population')) { console.log("The edited document has a field of population"); } }); – Fred Xie Commented Jan 21, 2018 at 23:19
- Please post it in your question with the correct formatting. Remove it from the ment as well. You can self edit..no problems with that ;-) – ZF007 Commented Jan 21, 2018 at 23:20
1 Answer
Reset to default 5As 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");
}
});