我正努力.then()与之合作,.on()但它只适用于.once().
我playersRef.on(...).then is not a function在尝试的时候得到了on()
所以我现在拥有的是:
let nodesRef = Firebase.database().ref('players')
let answers = {}
playersRef.on('value', (playersSnapshot) => {
playersRef.once('value').then((playersSnapshot) => {
playersSnapshot.forEach((playerSnapshot) => {
let playerKey = playerSnapshot.key
let answersRef = playerSnapshot.child('answers')
if (answersRef .exists()){
answers[playerKey ] = answersRef .val()
}
})
}).then(() => {
console.info(answers);
dispatch(setPlayerAnswers(answers))
})
})
Run Code Online (Sandbox Code Playgroud)
但我不喜欢它两次查询引用的事实.
each在这个例子中,我如何获得玩家的答案?什么是更正确的方法?因为我认为这不是Firebase开发人员想要的方式.
是什么原因导致它没有实现.on()?因为playersSnapshot.forEach(...).then它也不是一个函数...... 每当触发器时我怎么只执行一次.on('value')?