Reputation: 29
I am completely new to Firebase and I have got the data to save in the Firebase database, but I am not being able to retrieve it and show up in a table. My code is below. Any help will be greatly appreciated.
Here is the image of the firebase database:
Javascript:
/*jslint plusplus: true*/
var rootRef = firebase.database().ref().child("users");
rootRef.on("child_added", snap => {
var medname = snap.child("MedName").val();
var end = snap.child("End").val();
var start = snap.child("Start").val();
var type = snap.child("Type").val();
var Description = snap.child("description").val();
$("demo").append("<tr><td>" + medname + "</td><td>" + end +
"</td><td>" + start + "</td><td>" + type +
"</td><td>" + Description +
"</td><td><button>Remove</button></td></tr>");
}
Html:
<!-------------------------------------- Table -------------------------------------------->
<table>
<thead>
<tr>
<td>MedName</td>
<td>End</td>
<td>Start</td>
<td>Type</td>
<td>Description</td>
</tr>
</thead>
<tbody id=demo>
</tbody>
</table>
Upvotes: 1
Views: 1129
Reputation: 515
Your code will work if you remove two major mistakes.
However, here is a rewrite without using variables:
var rootRef = firebase.database().ref().child('users');
rootRef.on("child_added", function (userRecord) {
var user = userRecord.val();
var row = $('<tr></tr>');
row.append('<td>' + user.MedName + '</td>');
row.append('<td>' + user.Start + '</td>');
row.append('<td>' + user.Type + '</td>');
row.append('<td>' + user.description + '</td>');
row.append('<td><button>Remove</button></td>');
$('#demo').append(row);
});
Also it is unclear whether you meant to use ES2015 or ES5. That could cause compatibility issues depending on where the code is being run but try to not mix them.
Here are the best Javascript style guides I've ever seen:
Upvotes: 1
Reputation: 115
first of all correct the error in the script. write #demo instead of demo in jquery . Hope this will help you completely
Upvotes: 1