无法从Firebase数据库中检索数据?

时间:2018-01-30 10:28:45

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

我是Firebase和JS的新手。我试图在存储在Firebase数据库中的网页上显示一些用户信息。

Data format is something as this image

这是我编写的Javascript代码基础教程。我无法从firebase数据库检索数据(电子邮件)到网页。

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


//Get elements
const txtEmail = document.getElementById('txtEmail');
const txtPassword = document.getElementById('txtPassword');
const btnLogin = document.getElementById('btnLogin');
const btnSignup = document.getElementById('btnSignup');
const btnLogout = document.getElementById('btnLogout');

//Add login event
btnLogin.addEventListener('click', e => {
  //Get email and password
  const email = txtEmail.value;
  const pass = txtPassword.value;
  const auth = firebase.auth();
  //Sign in event

  const promise = auth.signInWithEmailAndPassword(email,pass);

  promise.catch(e => console.log(e.message));

});


btnSignup.addEventListener('click', e => {
  const email = txtEmail.value;
  const pass = txtPassword.value;
  const auth = firebase.auth();
  //Sign in
  const promise = auth.createUserWithEmailAndPassword(email,pass).then(newUser => {
      firebase.database().ref('userProfile').child(newUser.uid).set({email: email });
    var userProfileRef = database.ref('userProfile');
    userProfileRef.on('value', function(snapshot) {
      snapshot.forEach(function(childSnapshot) {
        var childData = childSnapshot.val();
      });
    });
  });
  promise.catch(e => console.log(e.message));
});


btnLogout.addEventListener('click', e => {
  firebase.auth().signOut();
});



firebase.auth().onAuthStateChanged(firebaseUser => {
  if (firebaseUser) {
    var smail = "abcdefgh@gmail.com";
    document.cookie = smail;
    window.location = 'sample.html';
    btnLogout.classList.remove('hide');


  } else {
    console.log('not logged in');
    btnLogout.classList.add('hide');
  }
});
}());

1 个答案:

答案 0 :(得分:0)

将您的onAuthStateChanged更改为此

firebase.auth().onAuthStateChanged(firebaseUser => 
{
    if (firebaseUser) 
    {

        var userRef = database.ref().child("userProfile").child(firebaseUser.uid);
        userRef.on('value', function (snapshot) 
        {
            var childData = childSnapshot.val();
            console.log(childData.email);
            alert(childData.email);
            const body = document.getElementsByTagName()[0];
            body.innerHTML = '<h1>Email is  : ' + childData.email + '</h1>' + body.innerHTML;
        });

        var smail = "abcdefgh@gmail.com";           // I don't why you need this
        document.cookie = smail;                    // I don't why you need this
        window.location = 'sample.html';            //why you want to redirect
        btnLogout.classList.remove('hide');


    }
    else 
    {
      console.log('not logged in');
      btnLogout.classList.add('hide');
    }

});