Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase last record only

I want to append only the last record added to firebase to a table. I tried

firebase().database().ref()
  .orderByChild("dateAdded")
  .limitToLast(1)
  .on("child_added", function (snapshot) {
    $('.table').append(
      "<tr><td>" + snapshot.val().train + 
      "</td><td>" + snapshot.val().destination + 
      "</td><td>" + snapshot.val().frequency + 
      "</td></tr>"
    );
  });

The first time I add a new entry it works but the second new entry is added twice, third - three times, etc.

Is there a way that will only add each new entry only once?

like image 439
PaulaMacT Avatar asked Mar 20 '18 18:03

PaulaMacT


People also ask

What is orderBy in Firebase?

You can specify the sort order for your data using orderBy() , and you can limit the number of documents retrieved using limit() . Note: An orderBy() clause also filters for existence of the given field. The result set will not include documents that do not contain the given field.

How do I filter a Firebase realtime database?

We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be combined with one or more of the following parameters: startAt , endAt , limitToFirst , limitToLast , or equalTo .

How do I use startAt?

Add a simple cursor to a query Use the startAt() or startAfter() methods to define the start point for a query. The startAt() method includes the start point, while the startAfter() method excludes it. For example, if you use startAt(A) in a query, it returns the entire alphabet.

What is addListenerForSingleValueEvent?

addValueEventListener() keep listening to query or database reference it is attached to. But addListenerForSingleValueEvent() executes onDataChange method immediately and after executing that method once, it stops listening to the reference location it is attached to.

What is limit to first and last in Firebase?

Firebase offers several ways to filter data. Let us understand what limit to first and last is. limitToFirst method returns the specified number of items beginning from the first one. limitToLast method returns a specified number of items beginning from the last one. Our example is showing how this works.

How do I retrieve data from Firebase Database?

Firebase data is retrieved by attaching an asynchronous listener to a firebase.database.Reference. The listener is triggered once for the initial state of the data and again anytime the data changes. Note: By default, read and write access to your database is restricted so only authenticated users can read or write data.

How do I know when my data is committed to Firebase?

To know when your data is committed to the Firebase Realtime Database server, you can use a Promise . Both set () and update () can return a Promise you can use to know when the write is committed to the database. Callbacks are removed by calling the off () method on your Firebase database reference.

What happens when Firebase console downloads too many nodes from database?

In this StackOverflow answer, Frank van Puffelen writes that When it detects that it’s downloading too many nodes from your database, the Firebase Console stops using real-time mode and switches to read-only mode. In this mode it requires less work from the browser, so it is more likely that the browser will stay performant. What to do first?


1 Answers

It seems you need to read your data only once and not every time data is changed at the specified database reference

So you should do:

firebase().database().ref()
  .orderByChild("dateAdded")
  .limitToLast(1)
  .once("child_added....

Have a look at the documentation: https://firebase.google.com/docs/database/web/read-and-write#read_data_once

like image 165
Renaud Tarnec Avatar answered Nov 14 '22 23:11

Renaud Tarnec