title: How to export Firestore subcollection data to CSV
body: <p>I'm trying to export subcollection of a firestore collection to a CSV file with the following code.</p>
  
  <p>The npm used for this is "json2csv"</p>
  
  <pre><code>const functions = require('firebase-functions');
  const admin = require('firebase-admin');
  admin.initializeApp(functions.config().firebase);
  const json2csv = require("json2csv").parse;
  exports.csvJsonReport = functions.https.onRequest((request, response) =&gt; {
      const db = admin.firestore();
      const data = db.collection("collectionname").doc().collection("subcollection") //collection name ,documentid, subcollection
      return data.get().then((querySnapshot) =&gt; {
          var object = {}
          var jsondata = [];
          querySnapshot.forEach(doc =&gt; {
              object = doc.data();
              object['docid'] = doc.id; // must add this line after doc.data
              console.log("objectbefore", object)
              jsondata.push(object);
              console.log("objectafter", object)
              console.log(jsondata);
          });
          const csv = json2csv(jsondata);
          response.setHeader(
              "Content-disposition",
              "attachment; filename=files.csv"//file name.csv
          );
          response.set("Content-Type", "text/csv");
          return response.status(200).send(csv)
      }).catch((err) =&gt; {
          return console.log(err);
      });
  });
  </code></pre>
  
  <p>But the result that I get in the output CSV file is only the collection without the inner subcollections.</p>
  
  <p>is it possible to export subcollection data in this method? If not, how can I export subcollection data to CSV?</p>
  
  <p>edit #1: code is updated. This is the error I get
  Error: Data should not be empty or the "fields" option should be included</p>