I’m creating a MERN stack ecommerce application where I want send all user info along with jwt token but except password I’m ok with token part & I know how to send user but i don’t know how to exclude the password property while sending the user through res.json
enter image description here
3
Answers
You can use the aggregation or select method in the mongoose.
Modified Answer –
@prathamesh
You can change the default behavior at the schema definition level using the select attribute of the field:
password: { type: String, select: false }
Then you can pull it in as needed in find and populate calls via field selection as ‘+password’. For example:
Users.findOne({_id: id}).select(‘+password’).exec(…);
I use this way to save all attributes except password in another variable and then I show info.