I have array of sensor data uploaded to firebase on each specific timestamps on regular intervals as below.
To retrieve array of values from latest time stamp, I’m tracking latest timestamp by another key as below
.
I’m successful to get latest timestamp and subscribe to it for any changes.
From this timestamp I need to get array of data. This may be seems to be simple, but I’m not able to get a proper way of accomplishing this. My code is shown below. At first timestamp array of data is displayed, but on changing timestamp, array of data returns null.
I tried onValue instead of get, but on first load all data appears at a time same like as in get, but on consequent changes each array item appears one by one added instead of getting together.
please help to know where I’m wrong.
const last_timestamp =ref(db,dataroot + "lastreading");
onValue(last_timestamp, (snapshot) => {
const updatetimestamp = snapshot.val();
const tmstamp=snapshot.child("upload_timestamp").val();
document.getElementById("timestamp").innerHTML="Last timestamp: " + tmstamp;
const edata =ref(db,dataroot + "readings/" + tmstamp);
get(edata).then((querySnapshot) => {
document.getElementById("data").innerHTML=JSON.stringify(querySnapshot.val(),true) ;
})
.catch((err) => console.log("err"))
});