Получение данных из Firebase для веб не работает - PullRequest
0 голосов
/ 13 июня 2018

Я пытаюсь читать и показывать данные из моей базы данных Firebase разными способами, но не могу заставить их работать.В моей базе Firebase есть следующая структура: База данных Firebase

И это весь код js:

var config = {
    apiKey: "thecharactersoftheapikey",
    authDomain: "xyxyxyxyxyxyxy.firebaseapp.com",
    databaseURL: "https://xxxxxxxxxxx.firebaseio.com",
    projectId: "xyxyxyxyxy",
    storageBucket: "xxxxxxxxxx.appspot.com",
    messagingSenderId: "12345678"
  };
  firebase.initializeApp(config);

/*If user is logged or Not and get the user email to display*/ 
firebase.auth().onAuthStateChanged(function(user) {
    var name, email, picture;
    if (user) {
            name = user.displayName;
            uid = user.uid;
            email = user.email;
            //picture = user.photoURL;
            //document.getElementById("user-image").src = picture;
            document.getElementById("user-name").innerHTML = email;
    }else {
        document.getElementById("members-area").style.display="none";
    }
  });

  /*To Log out */
function singOut(){
    firebase.auth().signOut().then(function() {
    // Sign-out successful.
    location.href="http://localhost/xxx/index.html";
  }).catch(function(error) {
    // An error happened.
  });  
}


/*Retrieving data from the database*/ 
var usersRef = firebase.database().ref('users');
     usersRef.on('value', function(snapshot)  {
     snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
      console.log(childData);
     });
    });

В console.log ничего не отображается.

Примечание. Я уже пытался использовать «child_added» вместо «value», и он не работает.

1 Ответ

0 голосов
/ 15 июня 2018

Проблема заключалась в том, что правила в моей базе данных не разрешали чтение / запись из любого источника.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...