Firestore Synchronous Import Code

Introduction

{
"AA": {
"id": "AA",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageAA.jpg"
},
"BB": {
"id": "BB",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageBB.jpg"
},
"CC": {
"id": "CC",
"url": "https://firebasestorage.googleapis.com/v0/b/blahblah/imageCC.jpg"
}
}

Setup and Installation

Node Script

var admin = require("firebase-admin");
var serviceAccount = require("./your-service-account-secret-key.json");
var fs = require('fs');
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://your-project-id.firebaseio.com"
});
var firestore = admin.firestore();
var writeBatch = firestore.batch();
var obj = JSON.parse(fs.readFileSync('your-file.json'));
console.log("working…");
for (var key in obj) {
var ref = firestore.collection("your-collection-name").doc();
writeBatch.set(ref, obj[key]);
}
writeBatch.commit().then(function () {
console.log("done.");
})
.catch(function(error) {
console.error("Error writing batch: ", error);
});

Troubleshooting

--

--

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ronnie Royston

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver