Andrei Enache
Andrei Enache

Reputation: 517

Convert a FirebaseFirestore.DocumentSnapshot into a list/map in Node.js

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
});

Upvotes: 0

Views: 1103

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

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);
});

Upvotes: 1

Related Questions