Reputation: 13803
I know I can hide some properties from mongoose model using select when I performing find query like this:
Product.find()
.select("-__v")
.then((products) => {}
but when performing save()
, .select()
doesn't work, and give error
"TypeError: (intermediate value).save(...).select is not a function"
const user = await new User({
email: email,
password: hashedPassword
}).save().select("-password")
I want to remove the password from user object, after successfully saving the user object. how to do that using mongoose ?
Upvotes: 0
Views: 1161
Reputation: 7949
simply Update Your User Model with the following Property.
password: {
type: String,
select: false //prevent password to show in query results
},
Upvotes: 2
Reputation: 11770
.save()
is not a query you can't chain query helpers.
delete the property after you insert the document
let user = await new User({
email: email,
password: hashedPassword
}).save()
user = user.toObject();
delete user.password;
Upvotes: 1