0

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:

wow I really can't insert images

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?

Ashish
  • 6,791
  • 3
  • 26
  • 48
itsisaac19
  • 536
  • 3
  • 15

1 Answers1

1

I've found my issue. The problem is getting access to the database. I had my rules like so:

{
  "rules": {
    ".read": "auth.uid != null",
    ".write": "auth.uid != null"
  }
}

Which was the correct setup if you were an authenticated user. I read up on Firebase Permission Denied even though I didn't get an error in the console. I set the JSON to true like so:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

and that solved my problem.

It's really unfortunate that this silly mistake caused all this fuss. If we can't access the database, we should get an error in the console. I know that the listener on('value') will only return if there is a value there, but there should be a more practical listener that will let you know if you don't have access to the database. Especially for beginners like me, it's hard to keep track of everything else that's going on-that little things like this hang us up for no good reason.

itsisaac19
  • 536
  • 3
  • 15