firebase on('value') with await does not work as expected

前端 未结 1 1984
不思量自难忘°
不思量自难忘° 2021-01-11 12:39

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 upcomi         


        
1条回答
  •  清酒与你
    2021-01-11 13:24

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

    0 讨论(0)
提交回复
热议问题