Home > other >  how to create a json file from a firestore collection using javascript
how to create a json file from a firestore collection using javascript

Time:08-02

I have a collection in Firestore, and i want to create a button which saves the data fetched from a specific collection into a JSON file...using either angular or javascript.

Below is a link to a youtube vid to upload a Json file to a firestore collection, im trying to tweak it to download the firestore collection and save it into a JSON file instead... hope this helps

https://youtu.be/I11O0UVp8PQ

CodePudding user response:

Just fetch your data, stringify it with your desired format and save it with a filewriter.

import fs from 'fs';

const fetch = async () => {
    const documents = (await getDocs(collection(ref, 'your-collection'))).docs.map((doc) => doc.data());

    const json = new Blob([JSON.stringify(documents, null, 4)], { type: 'application/json' });

    fs.writeFile('/data.json', json, console.error);
}
  • Related