vendredi 20 octobre 2017

get all children of a specific child once using Firebase web

i have a firebase database as follows:

enter image description here

i need to choose a random statement. Due to difficulties in choosing a random record based on firebase 'push' keys (based on other SO answers) i have created a numeric key "n:1" etc so i can call based on a random number. Here is my code to retrieve a random statement:

      function getStatement() {
        var ref= firebase.database().ref('/totNumState/');
        ref.once('value').then(function(snapshot) { 
            var max = (snapshot.val());
            var randomIndex = Math.floor(Math.random() * max) + 1;
            var rootRef = firebase.database().ref(); 
            rootRef.orderByChild('data').equalTo('n:'+randomIndex).once("value")

              .then(function(snapshot) {
                console.log(snapshot.val());
              });
        }, function(error){console.error(error)});
    }

PROBLEM: i am getting a 'null' response to my search. MAIN QUESTION: how can i get all the children of a specific child in firebase web? SECONDARY QUESTION (related): why does firebase make me use (the resource hungry) 'orderByChild' when i know exactly the record i want to retrieve?

thanks




Aucun commentaire:

Enregistrer un commentaire