1

I'm writing a Firebase cloud function that creates a "Multi-player Room" object in the Real-time Database for my multi-player game. I use databaseReference.push() to write the room in the database with a unique ID, but I need to get that ID and store in the room object too. What I get:

{
   multiplayer_games:{
      -SOME_ID:{
         some_value:***,
         some_other_value:***
      }
   }
}

What I need:

{
   multiplayer_games:{
      -SOME_ID:{
         id:-SOME_ID,
         some_value:***,
         some_other_value:***
      }
   }
}

So how can I get that random push ID and use it?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Ameer Taweel
  • 949
  • 1
  • 11
  • 37

1 Answers1

4

If you want to generate a key you can use:

let key = admin.database().ref().push().key;
Diego P
  • 1,728
  • 13
  • 28