0

Everything works fine when I use value change listeners. But I wanna make a multiplayer matchmaking functionality for my game. Data gets duplicated when so many users connect at same time. So I want to use Firebase Transaction.

Never worked with Firebase Transaction before. So wrote this code to get the data. But every time it's returning null.

return db.ref('matched').transaction((data) => {
        if(data){
            console.log(data);
        }

    }).then((result) => {
        console.log(result.snapshot.val())
        return result
    }).catch((err) => {
        console.log(err)
    });

Data and result.snapshot.val() both value is null

Matched DB

enter image description here

Where am I doing it wrong?

Bucky
  • 1,116
  • 2
  • 18
  • 34
  • Can you be more specific about what exactly is null here? – Doug Stevenson Mar 09 '18 at 17:37
  • data and result.snapshot.val(), both are null @DougStevenson – Bucky Mar 09 '18 at 17:44
  • 1
    The initial value that you get passed in to your transaction callback is the client's estimate of the current value. The first time it calls you, this is frequently `null`. Your code will need to handle that case. See https://stackoverflow.com/questions/16359496/firebase-transaction-api-call-current-data-is-null and https://stackoverflow.com/questions/28811037/data-in-transaction-is-null – Frank van Puffelen Mar 09 '18 at 17:48

0 Answers0