Read and Write Data on the Web in Firebase

Tram Ho

Hello everyone, today I will share about how to read , write and delete data in firebase . This article is only for beginners, those who have master, then please help me.

To help me understand, please give me a specific example. This article will help us get a list of restaurants in Danang, then I will add, update and delete a particular restaurant. The restaurant has the following information: name of the restaurant, address, telephone number and classification (for example, a shop selling milk tea, selling seafood, selling barbecue …).

Okay, let’s get started!

1. Get a database reference

To read or write data from the database , I first need to refer to the firebase database as follows:

2. Reading and writing data

I have a database structure like this:

Read the records included in the collection

Function above Function not returned to me an array of this structure.

You can write a separate function as below to be able to import and use only:

Okay, I adjusted the get restaurant function as follows:

Add, update a record

Now to add a record , we use the firebase add() :

When updating a record we will pass the data and id of the record to be update and use update() :

Delete a record

For simple deletion we just need to use detete() and remember to pass the id of the record be deleted.

In addition, firebase also supports us with many features such as adding more than one record a time using transactions or batch , realtime database …. etc. Hopefully the above sharing will help everyone. See you in the next article.

Share the news now

Source : Viblo