简体   繁体   中英

How to put an object inside another object in firebase database programmatically?

I'm setting up a firebase database for handling some requests I receive from my application. I need to insert an object "player" inside another object "room" to handle the multiplayer lobbies.

For a better comprehension I also do a little schema using firebase realtime db - schema below.

I want that instead of "room 1" the database puts an unique id, same thing for "player 1" and the other. Do you have any advice to help me doing this?

]

Try this (using a "path" to point to where you need):

const updates = {}
const roomName = 'room1'
const playerName = 'player1'
const path = `chip-chop/rooms/${roomName}/${playerName}`
const playerData = {
  name: 'marco',
  points: 0,
}
updates[path] = playerData
await firebase.database().ref().update(updates)

Please note that this will update the data. This means that other fields will not be affected (whether that is a bad or good thing is up to you)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM