Mongoose "setter" acting like "getter"?
Using Mongoose with MongoDB and Node.js
Previously I had my UserSchema defined like this:
var UserSchema = new Schema({
username: { type: String, unique: true },
password: String,
email: { type: Email, unique: true, validate:/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ }
});
When my User was created I encrypted the password. Then I found out about "setters" and 开发者_JAVA百科I changed my UserSchema to this:
var UserSchema = new Schema({
username: { type: String, unique: true },
password: { type:String, set:encryption.Encrypt },
email: { type: Email, unique: true, validate:/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ }
});
I have made no other changes (that I can remember) and when trying to log in with a User I discovered that my password was wrong. When I looked at the data, the encrypted password sent back from the database was different than the one stored in the database. If I take out the "setter" from my UserSchema, it looks correct.
Now my problem with this is that "setters" are described like this:
As you can see above, setters allow you to transform the data before it gets to the raw mongodb document and is set as a value on an actual key.
To me, it looks like this Setter is actually acting like a Getter and transforming the data as it comes BACK from the database.
Am I completely misunderstanding this?
This was actually a bug in Mongoose that has since been fixed in Mongoose 2.0
精彩评论