CRUD Operations In Firebase using Async Await In Node.js

Ritik Gupta
4 min readMay 25, 2020

--

Hello Everyone this is my second blog on Firebase using node.js, here we will be discussing how to perform CRUD operations In Firebase using the async-await in node.js.

If you are new to Firebase and have no idea about how to connect to Firebase storage and Firestore, don’t worry just refer to my blog.

I hope you are now well aware of the Firebase and already had done all the necessary setup as explained in my previous blog.

Adding Data To Firebase

Before directly jumping to store data, lets first understand how data is being stored in the Firestore.

So basically cloud Firestore stores data in Documents, which in turn are stored in collections.

If you are familiar with node.js mongoose schema then understand this in the following manner, the collection of firebase acts as mongoose schema and each entry of the schema is like a document in firebase and each document can contain various fields such as email, name, password, etc.

Warning: Don’t mix Firebase schema to mongoose schema, they are different from each other. It is used above just for better understanding.

Code to add data to the Firebase:

let docRef=db.collection('user').doc('Ritik')let store_user=docRef.set({email: 'ritikgupta89369@gmail.com',password: 'hello123',});

This is the basic structure of the code that is required to add the data to Firebase.

Facing difficulty to understand the code?

Don’t worry we will go through each line of code.

db.collection(‘Collection Name’) creates the collection from the name provided (‘user’ in the above example).

.doc(‘doc_name’) creates the document from the name provided by the user(‘Ritik’ in the above example).

docRef.set({key:value}) stores each field in the given document.

It’s implementation using REST API and async-await:

OUTPUT:

We can also use the add method instead of the set method to add data. The difference between both is that the latter one does not require doc name/id, it auto-generates one itself.

It’s implementation using REST API and async-await:

OUTPUT:

Updating Data From Firebase

To update some fields of a document without overwriting the entire document, we use the update() method.

It’s implementation using REST API and async-await:

OUTPUT:

Deleting Document From Firebase

It's very easy to delete a document from Firebase, all we need to do is use the delete() method in the following way:

Reading Data From Firebase

We use the get() method to retrieve the data from the Firebase.

To get all the documents present in the collection we can simply use the following code:

Facing problem to understand the code for reading data from Firebase?

Don’t worry, we will understand the code line by line.

const users = await db.collection('user').get()

This line helps to get the ‘user’ collection from the database.

If we do console.log(users), we can see how our collection looks.

for (const user of users.docs){
.......
}

This line helps to iterate through the various documents that are present in the collection.

If we console.log(users.docs), this is how our documents look like:

To extract the data from these documents we use .data() with the documents.

OUTPUT:

If we need to get a specific document from the collection, then that can be done using the where method in the following manner:

OUTPUT:

Conclusion

So this was all about the CRUD operations in Firebase using async-await.

I hope this article was useful. If you have any suggestions or complaints, I’ll be glad to find out about it in the comment section.

Github Repository link for this blog:https://github.com/ritikgupt/medium-blog-firebase

Stay Safe and Happy Coding.

--

--

Ritik Gupta

Backend Developer, Competitive Programmer.DevOps and blockchain enthusiast. LinkedIn Profile: https://www.linkedin.com/in/ritik-gupta-494738178/