最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - firebase on('value') with await does not work as expected - Stack Overflow

programmeradmin8浏览0评论

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

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 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)
Share Improve this question edited Jun 3, 2018 at 3:28 mentallurg 5,2075 gold badges30 silver badges37 bronze badges asked May 23, 2018 at 18:56 ManspofManspof 35727 gold badges93 silver badges182 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

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

This works for me

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

评论列表(0)

  1. 暂无评论