【问题标题】:Firebase retrieve user number to webFirebase 将用户号码检索到网络
【发布时间】:2018-09-26 01:44:45
【问题描述】:

您好,我正在为我的应用构建一个小型仪表板。我对 Firebase 有点陌生,但它为我节省了很多时间。所以我想要做的是从数据库直接检索到 Web 仪表板的总注册用户数或将来稍后的任何其他相关数据。所以我没有做一个小的登录表单,我可以登录。可能有人有类似经历我在图片中添加了我的数据库的结构。

    // Initialize Firebase
var config = {
  apiKey: "...",
      authDomain: "...",
      databaseURL: "...",
      storageBucket: "...",
  messagingSenderId: "..."
};
firebase.initializeApp(config);

// Firebase Variables
var auth = firebase.auth();

// on state changed
auth.onAuthStateChanged(firebaseUser => {
  // check email
  if(firebaseUser){

    currentEmail.innerHTML = auth.currentUser.email
    currentEmail.style.display = 'block';
    singoutButton.style.display = 'block';
    //singupForm.style.display = 'none';
    signin.style.display = 'none';
    signinEmail.style.display = 'none';
    signinPassword.style.display = 'none';
    signintext.style.display = 'none';
    welcometext.style.display = 'block';
    window.location = 'index.html';
  } else{
    signintext.style.display = 'block';
    welcometext.style.display = 'none';
    signin.style.display = 'block';
    signinEmail.style.display = 'block';
    signinPassword.style.display = 'block';
    singoutButton.style.display = 'none';
    //singupForm.style.display = 'block';
    currentEmail.style.display = 'none';
  }

});

Firebase

【问题讨论】:

    标签: javascript html firebase firebase-realtime-database firebase-authentication


    【解决方案1】:

    要检索用户数,请尝试以下操作:

    var ref = firebase.database().ref("users");
    ref.once("value").then(function(snapshot) {
    var numberOfChildren = snapshot.numChildren();
    });
    

    假设users 是根节点的直接子节点。

    numChildren() 返回号码

    返回此 DataSnapshot 的子属性数。

    更多信息在这里:

    https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot#numChildren

    【讨论】:

    • 非常感谢我使用说并添加了 console.log(snapshot.numChildren());在控制台中显示确切的用户数之后。如何将结果附加到 HTML?
    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 2010-10-30
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多