I've set up hosting with Firebase.
Using Firebase with the Firebase Realtime Database, this is my code so far:
var config = {
// don't post keys publicaly
};
var database = firebase.database()
var userRef = database.ref('User');
userRef.on('value', (snapshot) => {
const data = snapshot.val();
console.log(data)
});
NOTE: When I have firebase.initializeApp(config)
after var config
is declared, it returns an error in the console:
Uncaught FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app).
I assumed the app was already initialized and didn't insert the initialization code.
The Firebase docs state:
The listener receives a snapshot that contains the data at the specified location in the database at the time of the event. You can retrieve the data in the snapshot with the val() method.
Here is my database:
This is the json array exported from the database:
{
"User" : "John Smith"
}
And when I run the code, it doesn't log anything in the console. Could this be a path issue?
I think var userRef = database.ref('User')
works.
What could be wrong here?