I'm trying to create a new collection, and then create a sub collection all at once. The code is being run when a user creates a new team and adds a players to the team. (both team and player are created/added at the same time) This is what I have attempted so far:
const batch = db.batch()
const collectionRef = db.collection('users').doc(user.uid).collection('teams').doc(id)
batch.set(
collectionRef.set({
teamId: id,
teamName: teamName
})
)
batch.set(
collectionRef.collection('players').doc(player.id).set(playerObject)
)
batch.commit().then(()=> {}).catch(err=> console.log(err))
The player object is defined like so:
{
name: 'John Doe',
id: '123',
imgs: ['img.png']
...
}
But whenever this runs, only the first write is executed, the sub collection is not created. I think this is because the collection does not exist, so might cause an issue.