0

How can one convert a FirebaseFirestore.DocumentSnapshot to a list/map for parsing thought it afterwards?

The number of the fields varies in each document so it can't be done manually.

Nothing useful in the documentation:

exports.userDetailsForm = functions.firestore.
document('responseClientDetails/{details}').onCreate((snap, context) => {

  const newValue = snap.data();
  const caseReference = snap.id;



  return Promise
});
Kevin Quinzel
  • 1,430
  • 1
  • 13
  • 23
Andrei Enache
  • 507
  • 2
  • 6
  • 15
  • `newValue` is just a plain old JavaScript object, so you can use whatever techniques you would normally use to iterate an object's properties and values. There is nothing special required by Cloud Firestore - it's just JavaScript. – Doug Stevenson Sep 02 '19 at 17:22

1 Answers1

1

As explained in the doc you refer to, a DocumentSnapshot will return "an Object containing all fields in the document".

If you want to transform this Object into a map, you may use some of the techniques described in this SO answer. For example:

var docRef = db.collection("cities").doc("SF");

docRef.get().then(function(doc) {
    if (doc.exists) {
        console.log("Document data:", doc.data());
        for (let [key, value] of Object.entries(doc.data())) {
          console.log(`${key}: ${value}`);
        }
    } else {
        // doc.data() will be undefined in this case
        console.log("No such document!");
    }
}).catch(function(error) {
    console.log("Error getting document:", error);
});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121