Firebase 3 - Can't retrieve data from database (Web)

风流意气都作罢 提交于 2019-12-24 00:48:39

问题


I'm working on a webapp using Firebase 3 (the latest version) for the first time but I'm having troubles with retrieving data on my page.

I've created a NodeJS test app from Heroku, then I'm trying to retrieve my data in JSON format (ref's path goes to my data array) saved in my Firebase realtime database. I've followed the doc, but without results.

Here's my code:

<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"></script>
<script>

// Initialize Firebase
var config = {
    apiKey: "*******",
    authDomain: "*******",
    databaseURL: "*******",
    storageBucket: "*******",
};

firebase.initializeApp(config);
firebase.database().ref('my/path/to/data').on('value', function(snapshot) {
    console.log('kk'); //no log in the console
});

</script>

There are no errors in the console, but no results too. It seems so simple to do but I can't figure out what I'm missing.


回答1:


This has been working in my application:

firebase.database()
  .ref(`/database/path`)
  .once('value')
  .then((snapshot) => { console.log('hi') });



回答2:


This is strange, but the below code works.

var user = firebase.auth().currentUser;

firebase.database().ref('users/' + user.uid + '/<your-object-name>').once('value', function(snapshot) {
    console.log('A----->');
    console.log('snapshot', JSON.stringify(snapshot.val()));
});

Replace <your-object-name> with your object.



来源:https://stackoverflow.com/questions/37817028/firebase-3-cant-retrieve-data-from-database-web

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!