Reputation: 30
I am trying to retrieve an user object from my mongodb database :
router.get('/', async (req, res) => {
var user = await User.findOne({ _id: '5fe30ba2d8f18b353ce6c7c2' }).select('+password +token');
// it's ok, I can display my password
console.log(user.password);
// I don't have my password
console.log(user);
res.status(200).send(user);
});
Here is my model :
const user = new mongoose.Schema({
username: {
type: String,
required: true,
trim: true,
unique: true
},
email: {
type: String,
trim: true,
required: true,
validate: isEmail,
unique: true
},
password: {
type: String,
required: true,
min: 6,
select: false
},
firstName: String,
lastName: String,
dateOfBirth: Date,
description: String,
city: String,
phone: {
type: String,
},
social: Social,
timeToSpend: {
type: String,
enum: [
'USER.TIME_TO_SPEND.VERY_LITTLE',
'USER.TIME_TO_SPEND.LITTLE',
'USER.TIME_TO_SPEND.PARTIAL',
'USER.TIME_TO_SPEND.THREE_QUARTER',
'USER.TIME_TO_SPEND.FULL'
]
},
isAvailable: {
type: Boolean,
default: false
},
isProfitableOnly: {
type: Boolean,
default: false
},
theme: {
type: Number,
default: 0
},
isAdmin: {
type: Boolean,
default: false
},
isConnected: {
type: Boolean,
default: false
},
isEmailConfirmed: {
type: Boolean,
default: false
},
token: {
type: String,
required: true,
select: false
},
skills: [
{
name: String,
rating: {
type: Number,
min: 1,
max: 5
}
}
]
}, {
timestamps: true,
versionKey: false
});
'token' field is not hidden but 'password' field is just hidden when I retrieve my user object.
I tried to modify my password field on the schema, I tried removing the 'select: false' property but it doesn't work either
I tried to modify my user object like that : JSON.stringify(user)
but it doesn't work either
Could you help me please?
Thank you!
Upvotes: 0
Views: 495
Reputation: 1226
try this
const user = await User.findById('5fe30ba2d8f18b353ce6c7c2').select('password token');
res.status(200).send({...user._doc})
Upvotes: 1