Vespas
Vespas

Reputation: 405

Firebase: Database reference 'on' method not running callback (javascript)

I'm tunning a query for data that may not exist . When this is the case, the callback is not run. As I understand from the docs, it should run and the snapshot.val() should be null, isn't it?

There's a stripped down example here: http://surfmaps.eu/trombone/case.html

There's a console.log inside the callback that is not executed.

Am I missing something here?

Bye and thanks, Luís

Code:

function getData(id) {
    var ref=firebase.database().ref("support/"+id); 
    console.log("In getData, looking for ",ref);

    // get support data from firebase
    ref.on('value',function (snapshot) {
        console.log("In Value");
        console.log(snapshot);
    });

    console.log("end getData, looking for ",ref);

}

// on startup
getData("abc");

Upvotes: 12

Views: 11279

Answers (3)

Gilberto B. Terra Jr.
Gilberto B. Terra Jr.

Reputation: 313

See Firebase Rules, You dont have permission to Access Firebase Database Collection.

Go to Firebase Console, Database, Rules to update:

{
  "support{
    ".read": true, // allow read
    ".write": false
  }
}

Upvotes: 1

chia yongkang
chia yongkang

Reputation: 782

Use

function(error) {
console.error(error);

to find the error. It should be because of

Error: permission_denied at /react: Client doesn't have permission to access the desired data.

change that by going to realtime Database -> rules -> change the read and write to true from false.

Upvotes: 0

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

You don't have permission to read the data. To see this, you can attach a completion listener and log the error it gives you:

var ref=firebase.database().ref("support/"+id); 
console.log("In getData, looking for ",ref);

// get support data from firebase
ref.on('value',function (snapshot) {
    console.log("In Value");
    console.log(snapshot);
}, function(error) {
    console.error(error);
});

console.log("end getData, looking for ",ref);

Doing so shows:

Error: permission_denied at /support/abc: Client doesn't have permission to access the desired data.

Upvotes: 25

Related Questions