Returning Firebase data in an Array

Asked

Viewed 145 times

-1

So I have a little problem that I can’t solve at all.
I’m using the firebase database in conjunction with an ESP8266 microcontroller.
The ESP8266 sends the sensor data to the firebase, and through an HTML page I want to view the data and create a graph.

The code snippet I use to access the database and return the data is this:


   var refTanque1 = db.ref("Dados tanque 1 (Caixa d'agua)");


   refTanque1.on('child_added', function(snapshot, prevChildKey) {
     let key = snapshot.val();

     valornivelT1 = key['2- Nivel do tanque'];
     valorvolT1 = key['3- Volume do tanque'];
     var data = valorvolT1; 
     dataArr = [];
     dataArr.push(data);

     console.log(dataArr);

   });

What I get back are several arrays with only one position in this format:
[9.97]
[9.96]
[9.97]
[9.86]
[9.92]
[9.97]

What I want to receive is an array with all these elements separated by a comma, for example:
[9.97, 9.96, 9.97, 9.86, 9.92, 9.97]

I have tried to use the push method, Join, and everything I imagined but always unsuccessful.
Thanks in advance for any help.

1 answer

0


Instead of using the on('child_added'), use the once('value'):

var refTanque1 = db.ref("Dados tanque 1 (Caixa d'agua)");

refTanque1.once('value', function(listaSnapshot) {
     dataArr = [];
     listaSnapshot.forEach(function(snapshot) {
         let key = snapshot.val();

         valornivelT1 = key['2- Nivel do tanque'];
         valorvolT1 = key['3- Volume do tanque'];
         var data = valorvolT1; 
         dataArr.push(data);
     })
     console.log(dataArr);
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.