Skip to content Skip to sidebar Skip to footer

Firebase On('value') With Await Does Not Work As Expected

I'm looking to build function to wait until all values in on('value') will be set and then go to the next line, in other words async function. let upcomingGamesList = await fireba

Solution 1:

Firebase's on() function will continuously listen to the location that you call them on. That means that they can give you data multiple times. Since a Promise can only resolve once, on() does not return a promise. And thus it can't be used with async/await.

In this case it looks like you'll want to use once(), which works pretty much the same but will only deliver a result once (and thus returns a promise):

let upcomingGamesList = await firebase.database().ref('UpcomingGames').once('value', snapshot => {
    upcomingGamesList = snapshot.val()
    console.log('upcoming t2',upcomingGamesList)
    return upcomingGamesList
})

Solution 2:

This works for me

exportconst deviceLogin = async (pin) => {
    var ref = firebase.database().ref(`/devices/${pin}`);
    const snapshot = await ref.once('value');
    return snapshot.val();
}

Post a Comment for "Firebase On('value') With Await Does Not Work As Expected"