Skip to content Skip to sidebar Skip to footer

Firebase .then With .on('value')

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:

Solution 1:

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.keylet 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?

Post a Comment for "Firebase .then With .on('value')"