Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node crashes when a duplicate data is inserted into mongodb

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

like image 972
Hans Garcia Avatar asked Sep 01 '26 22:09

Hans Garcia


1 Answers

I think you have not used await user.create.

before completing the DB insert operation you are returning response and later DB is throwing an error.

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 = await user.create({
      email: email,
      password: pword,
      firstName: firstName,
      lastName: lastName,
    });

    console.log("User created successfully: " + response);
  } catch (error) {
    if (error.status === "E11000") { // it could be .status, .code etc.. not sure
      return res.json({ status: "error", msg: "User already exist." });
    }
    return res.json({ status: "error" });
  }
});
like image 141
Rahul Sharma Avatar answered Sep 03 '26 13:09

Rahul Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!