Creating a CSV file with Node & JS object - MiguelFieira/AMO-HANDBOEK GitHub Wiki
For now i don't have a lot of fukkin time so here is the goddamn code you lazy fucks
/**
* A simple forEach() implementation for Arrays, Objects and NodeLists
* @private
* @param {Array|Object|NodeList} collection Collection of items to iterate
* @param {Function} callback Callback function for each iteration
* @param {Array|Object|NodeList} scope Object/NodeList/Array that forEach is iterating over (aka `this`)
*/
var forEach = function (collection, callback, scope) {
if (Object.prototype.toString.call(collection) === '[object Object]') {
for (var prop in collection) {
if (Object.prototype.hasOwnProperty.call(collection, prop)) {
callback.call(scope, collection[prop], prop, collection);
}
}
} else {
for (var i = 0, len = collection.length; i < len; i++) {
callback.call(scope, collection[i], i, collection);
}
}
};
const admin = require('firebase-admin');
let serviceAccount = require('./firebase-admin.json');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});
let db = admin.firestore(); // Firestore connection
const fse = require('fs-extra'); // Filewriter functionalities
const file = 'oof.csv'; // Name of file to be created
let i = 0; // Iteration count for foreach loop
let data = {}; // Object to push data into
let string = '"name","phone",\n'; // String to contain titles for the CSV file
// -------------------------------------------------
db.collection('environment').doc('VxwMiZ1QSMbOe73OdCF4').collection('contact').get() // The JSON where you get your data from
.then((snapshot) => {
snapshot.forEach((doc) => {
let info = doc.data(); // Collect data (this part is only for firebase + node method)
data[i] = {name: info.name, phone: info.phone}; // Store only your neccesary data from the json in an pre-defined object
i++; // Aad iteration to loop
})
})
.then(() => { // When all data is collected. If you have a single js object, you can start here
forEach(data, (value, index) => {
string = string + '"' + value.name +'","'+value.phone+'",\n'; // Append data to the string with correct titles in the beginning
})
})
.then(() => { // When string is complete
fse.outputFile(file, string); // Use FS to create the CSV file
})
.catch((err) => {
console.log('Error getting documents', err);
});
Make it work yourself, this is very specific code. Figure it out