0

I am querying an object from my db using mongoose, and I want to remove the password field.
I am executing:

delete user.password;

But the user object still contain the field.

User.findOne({
    'id': id
  },'-_id', function(err, user) {
    // if there are any errors, return the error before anything else
    if (err)
      return done(err);

    // if no user is found, return the message
    if (!user)
      return done(null, false);

    user.comparePassword(password).then((res) => {
      console.log(user);
      delete user.password;
      console.log(user);
      return res ?
        done(null, user) : done(null, false);
    })

});

Creating a new object and deleting own of its field does work properly in the same environment.

      var x = {a:1, b:2};
      console.log(x);
      delete x.a;
      console.log(x)

What am I doing wrong and how can I remove the password field?

itaied
  • 6,827
  • 13
  • 51
  • 86
  • are you sure that `delete user.password` doesnt trigger an error that you dont catch? maybe `user` object has non-configurable properties. – Avraam Mavridis May 28 '16 at 17:43
  • But I don't want to update the document in my DB, I just don't want to send the password back to the client, so I need to remove that field from the JSON object itself without accessing the DB. – itaied May 28 '16 at 17:53

0 Answers0