Get a specific object from a list in Firebase

0

I have a list of publications in firebase,

I am developing an algorithm to obtain a random number and with the power to request an object from lst_publications to firebase, my random algorithm tells me what position (number) of the object is the one I should request, therefore I do not know which key has this object, there is some way to get this data only with the position,

Example

      const randomIndex = Math.floor(Math.random() * count);
      const refPublicaciones = vm.rootRef.child('publicaciones/lst_publicaciones')
      refPublicaciones.objeto(2).on('value', objeto => {

        console.log(objeto.val());

      })

Is there something like that? Greetings.

    
asked by juan carlos sanchez 28.07.2018 в 01:28
source

2 answers

1

Use the startAt () and limit () query cursors that allow you to decide where to start bringing data and how many you want to bring, which in your case would be like

db.collection('lst_publicaciones')
          .orderBy('descripcion')
          .startAt(randomIndex)
          .limit(1);

I hope it helps you. Here's the documentation

    
answered by 28.07.2018 в 06:06
0

The only solution I found so far was to put a consecutive identifier to each publication, "id_publicacion" (1,2,3,4, ...)

and then make a filter for the publications, so that only the publication would return with the requested id_publication, here the code:

var random = Math.round(Math.random() * (count-1))+1;
refPublicaciones.orderByChild("id_publicacion").equalTo(random).on('child_added', publicacion => {
  this.lstPublicaciones.push(publicacion.val());
})
    
answered by 03.08.2018 в 01:36