Monday, 28 May 2018

firebase on('value') with await not works as expected

I'm looking to build function to wait until all values in on('value') will be set and then go to next line, in other word async function.
 let upcomingGamesList = await firebase.database().ref('UpcomingGames').on('value', snapshot => {
        upcomingGamesList = snapshot.val()
        console.log('upcoming t1',upcomingGamesList)
        return upcomingGamesList
    })
    console.log('upcoming t2',upcomingGamesList)
    let upcomingPreferences = upcomingGamesList.map(async(game) => {
        console.log(game)
        let GameId = game.GameId
        await firebase.database().ref(`GameNotificationPreferances/${GameId}`)
.orderByKey().equalTo(UserStore.user.uid).once('value', snapshot => {
            if (snapshot.val() != null || snapshot.val() != undefined) {
                conosle.log(snapshot.val())
            } else {
                console.log('not value')
            }
        })
        console.log(game)
    })

what that happened is the upcoming t2
        console.log('upcoming t2',upcomingGamesList)

is printed before upcoming t1
    console.log('upcoming t2',upcomingGamesList)

But I used await in this function
  let upcomingGamesList = await firebase.database().ref('UpcomingGames').on('value', snapshot => {
        upcomingGamesList = snapshot.val()
        console.log('upcoming t2',upcomingGamesList)
        return upcomingGamesList
    })

and it should wait until it finish and then go to next line
I want to wait until the function will finish and then get the updatedList with the change i did
 let upcomingGamesList = await firebase.database().ref('UpcomingGames').on('value', async(snapshot) => {
        upcomingGamesList = snapshot.val()
        updatededList = await upcomingGamesList.map(async(game) => {
            let GameId = game.GameId
            await firebase.database().ref(`GameNotificationPreferances/${GameId}`)
.orderByKey().equalTo(UserStore.user.uid).once('value', async(snapshot) => {
                if (snapshot.val() != null || snapshot.val() != undefined) {
                    game['reminderPressed'] = true;
                } else {
                    game['reminderPressed'] = false
                }
                console.log('GameId:',GameId, 'GameDetails:',game)
                return ({...game})

            })

        })


    })
    console.log('the updatedList is',updatededList)



from firebase on('value') with await not works as expected

No comments:

Post a Comment