MongoServerError: E11000 duplicate key error collection

I’m getting this error whenever i’m trying to log a user in using passportjs, i checked in mongo collection and there is no username the same !

/Users//my-blog/api/node_modules/mongodb/lib/operations/insert.js:53 return callback(new error_1.MongoServerError(res.writeErrors[0])); ^ MongoServerError: E11000 duplicate key error collection: blog.users index: username_1 dup key: { username: “undefined_undefined” }

Auth.js Code:

router.get("/facebook", passport.authenticate("facebook", { 
  scope: ["email"] }));

router.get("/auth/facebook/callback", passport.authenticate("facebook", {
  successRedirect: "http://localhost:3000/",
  failureRedirect: "/facebookLogin/failed"
}));

router.get("/facebookLogin/success", async (req, res)=>{
  if(req.user){
    const user = await User.findOne({provider_id: req.user.id, 
      provider: req.user.provider})
    if(user){
       res.status(200).json({
        success: true,
        message: "success",
        user: user
      })
      
    }else{
      const checkUserEmail = await User.findOne({email: req.user.email})
      if(checkUserEmail){
        res.status(401).json({
          success: false,
          message: "User already Exist with this email id",
        })
      }else{
        const user = await User.create({
          username: req.user.name.givenName+ "_" +req.user.name.familyName,
          firstName: req.user.name.givenName,
          lastName: req.user.name.familyName,
          email: req.user.emails[0].value,
          provider: req.user.provider,
          provider_id: req.user.id,
          profilePic: req.user.photos[0].value,
        });
         res.status(200).json({
          success: true,
          message: "success",
          user: user
        })
      }
    }
    console.log("CURRNT USER: ", user);
  }
})

router.get("/facebookLogin/failed", (req, res)=>{
  if(req.user){
    res.status(401).json({
      success: false,
      message: "failure",
    })
  }
})

What I’m missing here ?

your schema seems to have “unique” tag on “username” and also your request does not send “user.name.givenName” and “user.name.familyName” as you expected (typos?), but instead, they are null and thus you get “undefined_undefined”. the first user can get registered and then anyone else hits on this error.

1 Like

I don’t get it !! there is no typos in the code, now i’m getting the same error but index: email_1

MongoServerError: E11000 duplicate key error collection: blog.users index: email_1 dup key

I don’t get why it’s a problem I have the unique tag on “username” and “email”

Problem is not you having “unique” tag on them.

The problem is that you are sending wrong/null data for them.

  • req.user.name.givenName+ "_" +req.user.name.familyName results in undefined_undefined
  • req.user.emails[0].value possibly becomes undefined
  • and I suspect your other fields try to extract from req.user are also undefined
3 Likes

It seems you are using Node.js and Express is your server.

In that case, your "POST"ed data is carried over in req.body.

  • use req.body.user.email if your data is this format: {user:{name:"...",email:"..."}}
  • use req.body.email if your data is in this format: {name:"...",email:"..."}
2 Likes