从Javascript中的Firebase数据库中的查询结果中获取特定子项的值 [英] Get the value of a specific child from the result of a query in a Firebase database in Javascript

查看:79
本文介绍了从Javascript中的Firebase数据库中的查询结果中获取特定子项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase上有这个示例数据库:

I have this sample database on Firebase:

示例数据库

我有一个index.html,它有两个输入文字:

I have an index.html that have these two input text:

...

<!--     LOGIN      -->
<div class="login-form" id="login">
  <h2>Login Form</h2>
  
  <input type="text" name="username" id="username" placeholder="Username">
  <input type="password" name="password" id="password" placeholder="Password">
  
  <a class="btn-login" id="btn-login">Login</a>

...

我想选择一个与inputText具有相同用户名的孩子,然后我想验证密码是否匹配。

I want to select a child that have the same "Username" of the inputText and then I want to verify if the password match.

我在index.js文件中尝试过这些查询:

I've tried with these query in the index.js file:

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);

      dip.equalTo("Password").on("value", function(child) {
        console.log(child.val());
      });
    });
  });
});

第一个console.log( snapshot.val())显示我搜索的具有用户名的孩子的正确选择:

The first "console.log(snapshot.val())" show the right select of the child with the "Username" that I search for:

第一个consoleLog

First consoleLog

但第二个console.log(child.val())返回 null。

But the second "console.log(child.val())" return "null".

任何人都可以帮助我吗?

Can anyone help me?

推荐答案

检查是否您在第二个查询中返回的快照存在。它将告诉您输入密码的用户是否存在于数据库中,否则它将返回null。

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])
                }
             })
        }
    });
  });
});

这篇关于从Javascript中的Firebase数据库中的查询结果中获取特定子项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆