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.
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:
database.once()
.snapshot.forEach(...)
). If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With