Creating Firebase Realtime database and fetch the data in simple JavaScript

Last modified date

Comments: 0

Firebase is the No SQL database to connect to any platform to communicate faster and efficiently. No SQL means where there is no sql queries and the database is basically not relational. Relational database is basically in form of tables and columns. In this we will familiar with how to Creating Firebase Realtime database and fetch the data in simple JavaScript stored in Firebase.

What is Firebase database –

If you are a google user you can just go to https://console.firebase.google.com/ by logging in by any google account and create firebase project. In left menu there is Realtime Database, where you will be creating realtime database. I assume you are aware how to create a project in the firebase.

After creating database you can create node in json format just like below –

here chat, friends and users nodes are created manually. You can create it from you code in any language using SDK for that language. We are using here in JavaScript to just fetch the records.

here is the JavaScript code to fetch records in the above nodes. I am using example of friends.

Fetch Records from Friends Node –

below is the code to fetch the data in simple JavaScript with the library-

<script src="https://www.gstatic.com/firebasejs/7.10.0/firebase-app.js"></script> //main firebase library
<script src="https://www.gstatic.com/firebasejs/7.10.0/firebase-auth.js"></script> //firbase auth for users
<script src="https://www.gstatic.com/firebasejs/7.10.0/firebase-database.js"></script> //firebase database 

    firebase.database().ref('friends').child(node_inside_parent_node).once('value', function (snap) {
        var user = [];
        $.each(snap.val(), function (key, value) {
           console.log(value);
      });
    });

snap.val() returns the nodes OR key value pair inside the friends database as highlighted in the picture. you can check in the JavaScript console

According to these data you can get the data from each node you created or added from your code and manually.

Accessibility Methods for FireBase –

  1. database() : firebase.database() is the base function which return the instance of the firebase relatime database
  2. database().ref(param) : firebase.database().ref(param) function return the object of the node you created in the realtime database. param is the parameter which is the main node name like ‘friends’ or ‘users’. We can call it as parent node.
  3. child(key) : child() method is used to get some child node on the parent node passed in the ref() method. key parameter in child() method is the name of that child node.
  4. orderByChild(‘node’) : If you want to sort or order the records you are fetching by any node, this method is used.
  5. once(‘value’,callback()) : once method is used to return the only instance. If there are more than one node with same name then the first node will be return in that case using once method. callback function is function to write your own code to action on the return values. see the example below

firebase.database().ref(‘friends’).child(myFirebaseUid).orderByChild(‘last_message_date’).once(‘value’, function (snap) {

firebase.database().ref(‘friends’).child(‘FJuMkIlM6AU2rX5sbq5UPk0vH243‘).orderByChild(‘last_message_date’).once(‘value’, function (snap) {
// return the node of FJuMkIlM6AU2rX5sbq5UPk0vH243 with all the inner node
});

So Hope you are clear about the firebase database and integrating with javascript from post.

Techspook

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment