简体   繁体   中英

How to get the return value of Firebase realtime database

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();
    }
        );
    }

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM