How to get the return value of Firebase realtime database

Viewed 151

It seems like I can't get the value of the data out of the function, when I use console.log inside the function it does work but I can't get the value out of the function.

function getFromDB() {
    const ref; //the db ref
    ref.on("value", data => {
    return data.val();
    }
        );
    }
1 Answers

You can try something like the following.

function getFromDB() {
    var tokenRef = firebase.database().ref('accounts').child('somefield');
    const value = tokenRef.once('value').then(function (snapshot) {
        return snapshot.val();
    });
console.log('value', value);
Related