I'm trying to create a User model, that has an unique username. Here's the code for it:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var UserSchema = new Schema({
username: String,
password: String,
});
UserSchema.virtual("password_confirmation").get(function() {
return this.pw_conf;
}).set(function(value) {
this.pw_conf = value;
});
UserSchema.path("username").required(true);
UserSchema.path("password").required(true);
UserSchema.pre("save",function(next, done) {
var self = this;
mongoose.models["User"].findOne({username : self.username},function(err, user) {
if(user) {
self.invalidate("user","username must be unique");
}
done();
});
next();
});
UserSchema.pre("save",function(next) {
if(this.pw_conf !== this.password) {
next(new Error("Must specify the password confirmation"));
}
else {
next();
}
});
module.exports = mongoose.model("User",UserSchema);
I was also testing to see if the uniqueness works:
var User = require("./users"),
mongoose = require("mongoose");
var u = new User();
mongoose.connect('mongodb://localhost/my_database');
u.username = "me";
u.password = "password";
u.password_confirmation = "password";
u.save(function(err) {
if(err) {
console.log(err);
}
mongoose.disconnect();
});
Problem is, it doesn't. Each time I run the code, I get a new object created. I am aware that there are probably other ways of ensuring uniqueness, but I'd like to do it in this way. Shouldn't I be calling done
after I handle the result of the findOne
method? Am I calling next
wrong?
http://mongoosejs.com/docs/api.html#schematype_SchemaType-unique is the way to go. It uses actual MongoDb indexes to make sure that your field is unique. No need for .pre
middleware.
Enjoy!
Two possibilities:
Your self.invalidate
call should be referencing "username"
instead of "user"
. If that doesn't fix it, you can fail things explicitly by passing an Error object to done
if you want to abort the save operation:
UserSchema.pre("save",function(next, done) {
var self = this;
mongoose.models["User"].findOne({username : self.username},function(err, user) {
if(err) {
done(err);
} else if(user) {
self.invalidate("username","username must be unique");
done(new Error("username must be unique"));
} else {
done();
}
});
next();
});
There's a really good plugin for mongoose that's really easy to install and use. The documentation is excellent and it worked for me first time.
But there is an issue with re-saving.
Have you considered using an asynchronous validator to catch the error?
UserSchema.path('username').validate(function (value, done) {
User.count({ username: value }, function (error, count) {
// Return false if an error is thrown or count > 0
done(!(error || count));
});
}, 'unique');