Using Mongoose with MongoDB and Node.js
I used to define my UserSchema as follows:
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 learned about " setters " and 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 did not make any other changes (which I remember), and when I tried to log in with the user, I found that my password was incorrect. When I was looking at the data, the encrypted password sent back from the database was different from the encrypted password in the database. If I pulled the βsetterβ out of my UserSchema, it looks right.
Now my problem is that the "setters" are described as follows:
As you can see above, setters allow you to convert data before it gets into the original mongodb document and is set as the value on the actual key.
It seems to me that this setter actually acts like a Getter and converts the data since it comes BACK from the database.
Am I completely misunderstanding this?