Why don't you want to check for existing username directly in the MongoDB query like below.
try{
const existingUser = await mongodb
.getDb()
.db()
.collection("users")
.findOne({ userName: req.body.userName });
if (existingUser) {
return res.status(400).json({ error: "Username is already in use" });
}
// rest of the code
} catch (error) {
res.status(500).json(error);
console.log(error);
}