Skip to content Skip to sidebar Skip to footer

Get The Value Of A Specific Child From The Result Of A Query In A Firebase Database In Javascript

I have this sample database on Firebase: Sample database I have an index.html that have these two input text: I want to select a child that have the same 'Username' of the inputT

Solution 1:

Ok, I've find a solution (thanks to Himanshu), maybe not the best solution, so if anyone has any suggestion to improve it please leave a reply:

var firebaseRootRef = firebase.database().ref();
var personale_Ref = firebaseRootRef.child('DatabaseTirocinio/Personale');

$(function() {
  $('#btn-login').click(function() {
    var id_user = $("#username").val();
    var id_password = $("#password").val();

    personale_Ref.orderByChild("Username").equalTo(id_user).on("value", function(snapshot) {

      var dipendente = snapshot.val();
      var dipKey = Object.keys(dipendente);
      var k = dipKey[0];

      if (dipendente[k].Password == id_password) {
        console.log("Ok");
      } elseconsole.log("Wrong password");
    });
  });
});

Solution 2:

Check if the snap you are returning in the second query exists. It will tell you if the user with the password that has been entered exists in the database if not it will return null.

var firebaseRootRef = firebase.database().ref();
var personale_Ref = firebaseRootRef.child('DatabaseTirocinio/Personale');

$(function() {
  $('#btn-login').click(function() {
    var id_user = $("#username").val();
    var id_password = $("#password").val();

    personale_Ref.orderByChild("Username").equalTo(id_user).on("value", function(snapshot) {
      console.log(snapshot.val());

      var dip = personale_Ref.child(snapshot.key);
        if(snapshot.exists()){
             Object.keys(snapshot.val()).map(k => {
                if(k == "Password"){
                  console.log(snapshot.val()[k])
                }
             })
        }
    });
  });
});

Post a Comment for "Get The Value Of A Specific Child From The Result Of A Query In A Firebase Database In Javascript"