Get only new data that is added to firebase

0

How I recover in real time only the data that is being registered as new in my database.

According to the documentation it is child_added but it is executed even if there is no saved data and if there is previous data it loads them all.

firebase.database().ref('usuarios').orderByChild('id').equalTo('jsstoni').on('child_added', function(snapshot)
    
asked by jsstoni 31.01.2018 в 02:55
source

2 answers

0

To track the elements added from a certain reference point and without obtaining the previous records, you can use endAt() and limit() , for example, to obtain the last record:

// recuperar el ultimo registro de 'ref'
ref.endAt().limit(1).on('child_added', function(snapshot) {

   //todos los registros despues del ultimo continuan para invocar esta funcion
   console.log(snapshot.name(), snapshot.val());

});

Another option may be to use limitToLast() and limitToFirst() , which replace limit() .

// recuperar el ultimo registro de 'ref'
ref.limitToLast(1).on('child_added', function(snapshot) {

   // todos los registros despues del ultimo continuan para invocar esta funcion
   console.log(snapshot.name(), snapshot.val());
   // obtener la ultima llave insertada
   console.log(snapshot.key());

});

And finally I attached this link where there are some example codes: link

I hope it serves you.

    
answered by 01.02.2018 в 06:28
0

You can monitor in real time the nodes that you specify in your database, for example:

var commentsRef = firebase.database().ref('usuarios/registros');
commentsRef.on('child_changed', function(data) {
    var clave = data.key;
    var texto = data.val().nombre;
    var autor = data.val().email;
  });

In the example, we are monitoring the user registers node and this will bring us the information of each new change that is made in said node, in the same way we can monitor when there are new registers or deletions in said node, for example:

 //Si Agrego un nuevo Usuario
  commentsRef.on('child_added', function(data) {
    var clave = data.key;
    var texto = data.val().nombre;
    var autor = data.val().email;
  });

  //Si se elimina un usuario
  commentsRef.on('child_removed', function(data) {
    var clave = data.key;
  });
    
answered by 11.02.2018 в 02:31