Dale McCaughan
Dale McCaughan

Reputation: 77

How to read values from a Firebase Database and store them into an HTML table

I have a Firebase Database that stores two values distance and a timestamp. I'm able to read the distances, but un able to read to more than 1 timestamp at a time.

    for (var i = 0; i <= 10; i++) {
        ref.child("distance").child(i).once("value").then(function(snapshot) {
            test = snapshot.val();
            test1 = JSON.stringify(test).replaceAll(/[^a-zA-Z0-9.]/g, "");
            myCreateFunction(test1, i+"");
        });
        ref.child("time").child(i).once("value").then(function(snapshot) {
            time1 = snapshot.val();
            console.log(time1);
            myCreateFunc(time1["0"], i+"");
        });
    }

    function myCreateFunction(test1, i) {
        var table = document.getElementById("myTable");
        var row = table.insertRow(-1);
        var cell1 = row.insertCell(0);
        var cell2 = row.insertCell(1);
        cell2.id = i;
        cell1.innerHTML = test1;
    }

    function myCreateFunc(time1, i) {
        document.getElementById(i).innerHTML = time1;
    }

This is a picture of my database

This is a picture of my database

This is the picture of the error that we get on our webpage

This is the picture of the error that we get on our webpage

Upvotes: 2

Views: 721

Answers (1)

Alireza Manashty
Alireza Manashty

Reputation: 101

I think you are only sending your first element of the array to the HTML create table:

myCreateFunc(time1["0"], i+"");

instead of something like this: myCreateFunc(time1, i+"");

Let me know if this solved your problem!

Upvotes: 2

Related Questions