I understand that in your scenario, several users can have the same room number.
You can always check if a room with that number exists by performing a query such as:
// Generate new room number
let newRoomNumber = generateNewRoomNumber();
const docRef = db.collection('users');
const snapshot = await docRef.where('room', '==', newRoomNumber).get();
if (snapshot.empty) {
console.log('Creating new room');
createNewRoom();
return;
}
Otherwise, the link that @Frank van Puffelen shared shows various ways to address this and also has some nice references
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…