[Vuejs]-Vue and Firebase RTDB connection

0👍

the ref method only receives one parameter.

dbReadSettings() {
  console.log("I'm here");
  let settings = firebaseDb.ref('userSettings/');

  settings.on('value', (snapshot) => {
    const data = snapshot.val();
    console.log(data)
  }, (error) => {
     console.log(error)
  })
}

0👍

That first callback you have is not needed, and will in fact never be called. Since you attach the listener inside that callback, the listener will never be attached.

You can easily check this by adding an extra log line:

console.log("I'm here");
let settings = firebaseDb.ref('userSettings/', function(error) { console.log(error) });
    console.log("Now I'm here");
    settings.on('value', (snapshot) => {
        const data = snapshot.val();
        console.log(data)
    }, function(error) {
        console.log(error);
    });
})

While you’ll see:

I’m here

You’ll never see:

Now I’m here

To fix this, remove the extraneous callback and just attach the listener right after you create the reference (which is not an asynchronous operation):

console.log("I'm here");
let settings = firebaseDb.ref('userSettings/')console.log(error) });
settings.on('value', (snapshot) => {
    const data = snapshot.val();
    console.log(data)
}, function(error) {
    console.log(error);
});

Leave a comment