Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display data from firebase to HTML table

Tags:

I would like to ask some help, I'm a front-end developer and we currently have a project/task that is all about the back end. My team members decided to used firebase as a database in our web application. I can now send data to the database but I have a problem, I cant retrieve the data from firebase to the web application. I am planning to retrieve data and display it in my table (HTML).

This is my code in my javascript for retrieving data from firebase:

var database = firebase.database().ref().child('Tasks');
database.once('value', function(snapshot){
    if(snapshot.exists()){
        var content = '';
        var TaskTitle = snapshot.val().TaskTitle;
        var JobId= snapshot.val().JobId;

        snapshot.forEach(function(data){
        });

        content = '<tr>';
        content += '<td>' + TaskTitle + '</td>'; //column1
        content += '<td>' + JobId + '</td>';//column2
        content += '</tr>';
    }

    $('#ex-table').append(content);
    console.log(snapshot.val());
});

And this is my code in my HTML table to display the data from the database:

<table class="table table-striped" id="ex-table">
    <thead class="thead-inverse">
        <tr>
        <th>Task Title</th>
        <th>Category</th>
        <th>Date</th>
        <th>Status</th>
        </tr>
    </thead>
    <tbody>
        <tr id="tr">
        <td id="titleTask"></td>
        <td id="categoryTask"></td>
        <td id="dateTask"></td>
        <td id="statusTask"></td>
       </tr>
   </tbody>

I cant actually see the data that I have retrieved using the console in my web browser in chrome.

and it's displaying this: Data displayed in console while in my web app it's displaying like this: undefined

Can someone please help me, I'm new to everything your help is so much appreciated.

like image 570
missNobody Avatar asked Jan 18 '18 00:01

missNobody


1 Answers

Your nesting is a bit wonky, and unfortunately it matters quite a lot where you put all the bits:

var database = firebase.database().ref().child('Tasks');
database.once('value', function(snapshot){
    if(snapshot.exists()){
        var content = '';

        snapshot.forEach(function(data){
            var TaskTitle = data.val().TaskTitle;
            var JobId= data.val().JobId;
            content += '<tr>';
            content += '<td>' + TaskTitle + '</td>'; //column1
            content += '<td>' + JobId + '</td>';//column2
            content += '</tr>';
        });

        $('#ex-table').append(content);
    }
});

In steps:

  1. This loads the tasks from Firebase, with database.once().
  2. Then if there are any tasks (one level indented), it loops over them (with snapshot.forEach(...)).
  3. In that callback (so one more level indented) it takes the title and job id for that specific task, and adds it to the HTML string it is building.
  4. The finally it adds the HTML to the table.
like image 107
Frank van Puffelen Avatar answered Sep 20 '22 13:09

Frank van Puffelen