【发布时间】:2016-11-03 12:57:46
【问题描述】:
我正在尝试让 .then() 与 .on() 一起使用,但它仅适用于 .once()。
当我尝试使用on() 时,我得到了playersRef.on(...).then is not a function
所以我现在拥有的是:
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))
})
})
但我不喜欢它两次查询 ref 的事实。
在此示例中,我将如何获得each 玩家的答案?什么是更正确的方式?因为我认为这不是 Firebase 开发人员的本意。
.on() 没有实现它的原因是什么?因为playersSnapshot.forEach(...).then 也不是一个函数...当.on('value') 触发时,我如何只执行一次?
【问题讨论】:
标签: javascript firebase firebase-realtime-database