I talked about how even though I should have never cheated I was able to learn from the experience and eventually escape an abusive situation. The story was far from my best work. I posted it in a…
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.
This installs Firebase admin package in the current directory. Then create a file index.js
with the following code
Here’s a function in the index.js
file which returns a mock data
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.
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
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
Thanks:)
I have been coding with Go as a main language for about a year now. Looking back, I got mostly comfortable in writing Go after a few weeks, and pretty confident after a few months. I am still…
I am in my fifth week of Aviation Maintenance Technology (AMT) school. It’s a drastic change from my previous life, but from the first day, I have had exactly zero moments of doubt. Even when my week…
In a world of Tinder and Grindr, dating in 2018 without using apps like these almost seems impossible. But with so many daters struggling with online fatigue — and the routine of downloading and…