Write to cloud Firestore using node.js server
A simple guide to teach you how to create a Cloud Firestore project in Firebase and add data to it using a node.js app.
First of all, I am so happy that I have achieved this. Have been trying it for the whole weekend. Seriously it feels so good I feel like crying.
Alright, Emotions aside let’s get started.
So the need to do so came when I found out that to call an API other then that of google’s from Firebase functions we need to get a paid plan, otherwise do you can only call the data yourself (like we are gonna do now) and then save it to the cloud Firestore database using Firebase admin SDK.
First of all, make sure you have node and npm installed on your system. Also make sure that you have a project setup.
After that follow along.
Go to Firebase console, click the gear icon and select project settings.
Then go to Service Accounts tab -> Firebase Admin SDK then select node.js and Generate new private key.
After that download the JSON file containing your service account credentials. Also, rename the file to something easy to remember like serviceAccountKey
as we are gonna need that name later.
Now you can create a new directory for your firebase project and move that serviceAccountKey
to this folder.
Open the terminal in the current directory and use these commands to follow along.
$ npm install firebase-admin
This installs Firebase admin package in the current directory. Then create a file index.js
with the following code
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');//initialize admin SDK using serciceAcountKey
admin.initializeApp({
credential: admin.credential.cert(serviceAccount)
});const db = admin.firestore();
Here’s a function in the index.js
file which returns a mock data
function getDialogue(){//return a promise since we'll imitating an API call
return new Promise(function(resolve, reject) {resolve({
"quote":"I'm Batman",
"author":"Batman"
});
})
}
The function returns a promise token and not the JSON file directly, that is because we’ll be imitating an asynchronous call and waiting until the response is received. In our case the JSON data that we are generating locally. So the promise will tell that the data is coming I promise just wait.
Now let’s call this function and when the data arrives write it to the cloud Firestore collection.
getDialogue().then(result =>{
console.log(result);
const obj = result;
const quoteData = {
quote: obj.quote,
author: obj.author
};
return db.collection('sampleData').doc('inspiration')
.set(quoteData).then(() =>
console.log('new Dialogue written to database'));
});
See here we are using the .then()
method which only runs the code within when the function getDialogue
returns some response. Here we’re using the db instance of the cloud Firestore to set the data using the set()
method. Using set()
overwrites data at the specified location, including any child nodes.
Now go to the terminal and run the index.js
file
$ node index.js
Now go to your project in Firebase console and you should see the document and its fields added to the collection.
Remember that if you don’t have the collection or the document already created then this will also create that for you.
Here is the full index.js
file
If you like my work please consider supporting me
Thanks:)