Chapter:2 - Basic Writes (Ticket: User Management)

Hi,

In my implementation of addUser() as one of my experiments I tried to do:
insertOne({ email: email,
name: name,
password: password})
And surprisingly the test “it returns an error when trying to register duplicate user” passed.
I say surprsingly as I would expect that there will be created the second item (during the first insert _id gets value x, and during the second insert nothing (as I thought) is preventing from creating a new document with _id y!=x)
So how do we understand that it’s a duplicated item?

Thanks in advance,
Yury

//
static async addUser(userInfo) {
try {
let { name, email, password } = userInfo
await users.insertOne({ email: email,
name: name,
password: password})
return { success: true }
} catch (e) {
if (String(e).startsWith(“MongoError: E11000 duplicate key error”)) {
return { error: “A user with the given email already exists.” }
}
console.error(Error occurred while adding new user, ${e}.)
return { error: e }
}
}
/
/