Search code examples
javascriptfirebase-realtime-databasereal-time

How to structure and fetch time-series data from Firebase


I have a real-time database in Firebase in the following format:

Firebase Data Structure

I'm using javascript to fetch the data and plot it on my web browser. Problem is I can't get any data out of it. Found some codes in which they usually get one value, not a time series based data. The current fetching process I'm using is:

firebase.database().ref('edge-data').limitToLast(40).on('value', ts_measures => {

    let timestamps = [];
    let values = [];

    ts_measures.forEach(ts_measure => {
        console.log(ts_measure.val().timestamp, ts_measure.val().value);
        timestamps.push(moment(ts_measure.val().timestamp).format('YYYY-MM-DD HH:mm:ss'));
        values.push(ts_measure.val().value);
    });

    myPlotDiv = document.getElementById('myPlot');


    const data = [{
        x: timestamps,
        y: values
    }];

My plot opens but remains empty, though it seems to recognize the dates intervals. I'm not sure the structure my real-time database has now is the best option for this, but any help within the process will be of great value.

Thanks!


Solution

  • This change in my .js file solved all:

    ts_measures.forEach(ts_measure => {
            console.log(ts_measure.val());
            timestamps.push(moment(ts_measure.val().timestamp).format('YYYY-MM-DD HH:mm:ss'));
            values.push(ts_measure.val().temperatura);
        });