1

I'm working on a simple project of a web app. It gets data from Firebase realtime database. I've already managed to get data from it but only one, specific record. It works like this:

var hum = firebase.database().ref().child('Ostatni_pomiar');
hum.once("value", snap => {
    var wilgotnosc = snap.child("Wilgotnosc").val();
    $("#wilg").append(wilgotnosc.toFixed(1) + "%");
});

The case is, I have a device based on ESP32 which sends data to the database every 35 mins. I would like to get the last 10 records to show it on my web page as a table or whatever else.

Structure of the database looks like: database structure

So one record would be 16:15 = humidity, pressure, temperature. The second record would be 16:50 = humidity, pressure, temperature, And so on, and so on...

Any idea how to achieve that? :)

Thank you in advance!

Kresek
  • 55
  • 1
  • 7

1 Answers1

1

Welcome to SO!

firebase has a .limitToLast() function that you can chain on to your .ref().

database
  .ref('Parametry_powietrza')
  .child('19 May 2020')
  .limitToLast(10)
  .once('value', snap => {
    // snapshot data
  });

More details on that here: https://firebase.google.com/docs/reference/js/firebase.database.Reference#limittolast

blaytenshi
  • 312
  • 3
  • 11
  • Thank you very much for that tip! I thought there is something like this but I couldn't find it. I'll try to use it today evening, we will see how will it end :) – Kresek May 21 '20 at 08:33
  • Hi! Sorry, it took so long but finally, I've managed to take a look at it. Thanks to you I've took data from firebase, and I'm able to limit it to specific number of records. But do you have any idea how to format it to make it look better? :D I mean, I just don't know how to refer to that data in HTML to make possible change look of it using CSS, etc. Now it looks like - https://i.imgur.com/eAXewj8.png And this is code - https://pastebin.com/Hhwbk0SK – Kresek May 23 '20 at 13:06
  • 1
    Glad to hear you got it working! Mark the answer as accepted and create a new question. Stackoverflow isn't very good at mixing issues together. Let me know when you've created the new question and i'll do my best to help you answer it :) – blaytenshi May 23 '20 at 15:30
  • Ah, thanks for the next tip! :D I'm still learning how all this works here ;) – Kresek May 24 '20 at 10:31
  • 1
    My latest question - https://stackoverflow.com/questions/61984817/data-from-friebase-make-it-look-better :) – Kresek May 24 '20 at 10:49