node.js - Mongoose {unique:true} validator not working properly -


i trying catch mongodb 11000 error in test application when 2 emails both same, each time newly created user added database , not seeing error logged console. new mongodb,express stack in general, may missing obvious.

i receiving following error in mongod:

background addexistingtoindex exception e11000 duplicate key error index: database.users.$email_1  dup key: { : "asdf@gmail.com" } 2014-06-02t01:52:34.654-0400 [conn10] index build failed. spec: { v: 1, unique: true, key: { email: 1 }, name: "email_1", ns: "database.users", background: true } error: 11000 e11000 duplicate key error index: database.users.$email_1  dup key: { : "asdf@gmail.com" } 

here defined schema:

//***************************************** //define user schema var userschema = new mongoose.schema({     fullname: string,     email: {type: string, unique:true},     password: string,     createdon: date }); //define user model mongoose.model('user',userschema); //***************************************** 

here code enter database

exports.doregistration = function(req,res){      user.create({          fullname: req.body.register.fullname,         email: req.body.register.email,         password: req.body.register.password,         createdon: date.now()      }, function(err, user){          if(err){              console.log(err);                     if(err.code===11000)                           res.redirect( '/error );          }else{              console.log('success');         }          res.redirect('/');      }); } 


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -