Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
like image 333
Vespas Avatar asked Jan 13 '17 00:01

Vespas


Video Answer


2 Answers

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.

like image 79
Frank van Puffelen Avatar answered Sep 24 '22 07:09

Frank van Puffelen


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
  }
}
like image 27
Gilberto B. Terra Jr. Avatar answered Sep 23 '22 07:09

Gilberto B. Terra Jr.