问题
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