So I was trying to test my node server if it can handle insertion of duplicate data to mongodb and apparently it can't.
Here's the code:
app.post('/register', async (req, res) => {
const { email, password, firstName, lastName} = req.body
console.log(lastName)
console.log(typeof(lastName))
pword = await bcrypt.hash(password, 10)
try {
const response = user.create({
email: email,
password: pword,
firstName: firstName,
lastName: lastName
})
console.log("User created successfully: " + response)
} catch (error) {
return res.json({ status: 'error'})
}
})
and here's the error:
MongoServerError: E11000 duplicate key error collection: ApartmentDB.UserData index: email_1 dup key: { email: "asdasa" }
at C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\operations\insert.js:51:33
at C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\cmap\connection_pool.js:272:25
at handleOperationResult (C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\sdam\server.js:370:9)
at MessageStream.messageHandler (C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\cmap\connection.js:479:9)
at MessageStream.emit (node:events:390:28)
at processIncomingData (C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (C:\Users\Hans\Desktop\MobDev Final Project\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10) {
index: 0,
code: 11000,
keyPattern: { email: 1 },
keyValue: { email: 'asdasa' }
}
[nodemon] app crashed - waiting for file changes before starting...
I saw a similar post to mine saying that he was sending two responses, but I don't see that in my case