Firebase。然后用.on('value') [英] Firebase .then with .on('value')

查看:133
本文介绍了Firebase。然后用.on('value')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 .then()来处理 .on(),但它只起作用与 .once()



我得到 playersRef.on(...) .then不是函数,用 on()



我现在拥有的是这样的:
$ b $ pre $ let nodesRef = Firebase.database()。ref('players')
(值),(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()
}
})
})。(()=> {
console.info(answers);
dispatch(setPlayerAnswers(answers))
})
})

但是我不喜欢这个查询两次的事实。



code>每个玩家在这个例子中的答案?什么会是一个更正确的方式?因为我不认为这是Firebase开发者的意图。



这是什么推理,它没有实现 .on ()?因为 playersSnapshot.forEach(...),那么也不是一个函数...只要执行一次 c $ c> .on('value')触发器

解决方案



我不完全确定您的问题的部分,所以只会回答我可以的部分。


对于 .on(),没有实现[ then()]的推理是什么


我们在Firebase JavaScript客户端中实施了对Promises的支持。承诺的定义是它只解决(或失败)一次。由于Firebase on()方法可以多次接收更新的数据,因此它不符合promise的定义。


无论何时触发 .on('value')

如果您只想响应一次事件,您应该使用 once()方法。



我不确定为什么你遇到 forEach()的问题。以下内容应该是有效的:

$ $ $ $ $ $ $ $ playerRef.on('value',(playersSnapshot)=> {
playersSnapshot。 forEach((playerSnapshot)=> {
let playerKey = playerSnapshot.key
let answersRef = playerSnapshot.child('answers')
if(answersRef .exists()){
答案[playerKey] = answersRef .val()
}
})
})

如果你在做这个工作时遇到了麻烦,你可以在jsbin中重现这个问题吗?


I'm trying to get .then() to work with .on() but it only works with .once().

I get playersRef.on(...).then is not a function, when trying it with on()

So what I have now is this:

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

But I don't like the fact that it is querying the ref twice.

How would I go about getting each player's answer in this example? What would be a more correct way? Because I don't think this is the way the Firebase devs intended this.

And what is the reasoning that it is not implemented for .on()? Because playersSnapshot.forEach(...).then is also not a function... How do I execute something only once whenever the .on('value') triggers?

解决方案

firebaser here

I'm not entire certain of parts of your question, so will only answer the parts that I can.

what is the reasoning that [then()] is not implemented for .on()?

We implemented support for Promises in the Firebase JavaScript clients. The definition of a promise is that it resolves (or fails) only once. Since the Firebase on() methods can receive updated data multiple times, it does not fit in the definition of a promise.

How do I execute something only once whenever the .on('value') triggers?

When you want to respond only once to an event, you should use the once() method.

I'm not sure why you're having problems with forEach(). The following should work:

playersRef.on('value', (playersSnapshot) => {
   playersSnapshot.forEach((playerSnapshot) => {
      let playerKey = playerSnapshot.key
      let answersRef = playerSnapshot.child('answers')
      if (answersRef .exists()){
        answers[playerKey ] = answersRef .val()
      }
   })
})

If you're having trouble making this work, can you reproduce that problem in a jsbin?

这篇关于Firebase。然后用.on('value')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆