Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
delete operator not functioning as expected in node.js

In my node.js express application I am retrieving a user from my database

const newUser = await User.create({
      username,
      password,
      email,
      avatar,
})

but before sending the response with the user object I want to remove the password.

delete newUser.password;
return res.status(200).json({ token, user: newUser });

but in my response the password is returned.

console.log(JSON.stringify(newUser)) returns:

{"_id":"11111111","username":"dylan","email":"dylan@email.com","admin":true,"password":"******"}
about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

query return value is document not javascript object

  • Documents have a toObject method which converts the mongoose document into a plain JavaScript object.
  • first convert it to object and then use delete on each property you want

also with mongoose it can done more properly and automatically

User.methods.toJSON = function () {
  const user = this;
  const userObj = user.toObject();
  delete userObj.password;
  return userObj;
};

every time you send this document as response it convert to JSON and every time that JSON.strigify() is call on a document it call this toJSON() method

about 4 years ago · Santiago Gelvez Denunciar

0

Maybe you can also do

delete newUser._doc.password 
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda