Skip to content

Instantly share code, notes, and snippets.

@timstermatic
Last active July 31, 2016 12:46
Show Gist options
  • Save timstermatic/6163577 to your computer and use it in GitHub Desktop.
Save timstermatic/6163577 to your computer and use it in GitHub Desktop.
Async unique validation with expressjs and mongoose
var mongoose = require('mongoose')
,Schema = mongoose.Schema
,ObjectId = Schema.ObjectId;
var userSchema = new Schema({
email: String,
password: String,
created: { type: Date, default: Date.now }
});
User = mongoose.model('User', userSchema);
module.exports = User
User.schema.path('email').validate(function (value, respond) {
User.findOne({ email: value }, function (err, user) {
if(user) respond(false);
});
}, 'This email address is already registered');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment