Home > Blockchain >  select a field then send the response without the field (omit the field the right & safe way)
select a field then send the response without the field (omit the field the right & safe way)

Time:07-04

I have the following schema:

const userSchema = new mongoose.Schema({
  // long long schema, email, phoneNum ... etc
  name: String,
  password_createTime: {
    type: Date,
    select: false
  }
})

And inside the first middleware in my Express app, I have the following code:

const userDoc = await User.findById(THE_USER_ID).select(' password_createTime')
if (userDoc.password_createTime > someDate) return next(new Error('the password got changed, your token is expired'))
// userDoc.password_createTime = undefined            
  • Related