user4285359 user4285359 - 10 months ago 92
HTML Question

Connecting Firebase database values to Firebase Web App

So I have a built a database and a web app both using Google's Firebase. Both the database and the web app have both been deployed and both work. I'm am trying to read value from the database using HTML and javascript. The value that is being displayed on my webpage is null for the two different ways I tried. I have attached a picture of the values saved in the database.

<script src="">
// Initialize Firebase
var config = {  
apiKey: "AIzaYUIYUkokngctrIbR9krB6Do",
authDomain: "",
databaseURL: "",
storageBucket: "",
messagingSenderId: "8298539696" 

//This was the first was we tried to do it
//var rootRef = firebase.database().ref();
//var dist = rootRef.child("dalemccaugh");
var ref = firebase.database().ref();
.then(function(snapshot) {
var test = snapshot.child("dalemccaugh").val();


//The was the second way we tried
function printData() {
dist.on("value", function(snapshot) {
}, function(errorObject) {
console.log("The read failed: " +errorObject.code);


enter image description here

Answer Source

"dalemccaugh" is the name of your database. This means it is the root of your JSON hierarchy. If you for example wants to read the "test" object, you can do it like this:

var ref = firebase.database().ref();

ref.child("test").once("value").then(function(snapshot) {
    var test = snapshot.val(); 

If you want to read the JSON object at the root level, you simply have to remove .child("test") from your reference.