Given the following user model:
const UserSchema = new mongoose.Schema({
id: Number,
userID: {
type: String,
unique: true
},
userName: String,
password: {type: String, select: false},
isAdministrator: {
type: Boolean,
default: false
}
}, {
timestamps: true
});
This model ensures that the password is not returned in the event of a request.
How do I change the UserModel so that the password is returned for Route A and not for Route B?
For instance:
router.get('/A', [returns password])
router.get('/B', [returns no password])
In your controller, where you execute your Mongoose Query, for example:
User.findOne({email})
you append a select function and put inside the parethesis the field you want to be selected, for instance:
User.findOne({email}).select("+password")
If you put a "-" it will exclude the field, if you do not put any symbol it will select that field, the "+" forces the selection of the fields that have a "select false" property defined in the schema.