I'm sending to firebase database information about humidity (value and time, showed in the link below and I have a question how can I get information about last measurement (value and time) in javascript because I want to show on the website information only about last measurement
CodePudding user response:
The way I'd do this is by attaching a timestamp to all of your documents (new Date().toISOString()
in javascript) and then checking to find the most recent one using firebase .where()
function in firestore: https://firebase.google.com/docs/firestore/query-data/queries.
This is assuming you're using firebase firestore. If you're using firebase realtime DB there should be a way to apply this concept of checking the most recent timestamps, just with different code.
CodePudding user response:
That'd be:
const ref = firebase.database().ref("Humidity");
const query = ref.orderByChild("time").limitToLast(1);
query.once("child_added").then((snapshot) => {
console.log(snapshot.val().value);
});
I recommend reading:
- the documentation on reading from Firebase
- the documentation on ordering/filtering data.