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

198
Vistas
Modify the specific key value of object before passing to res.json?

I have a function in node js that will find all the messages inside mongodb collection that I want to modify the value of the specific key before passing to res.json.

Collection

enter image description here

I want to modify the seen to be true before sending this to front end.

Solution I tried

export const getMessage = async (req, res) => {
  try {
    const message = await messageModel
      .find({
        conversationId: req.params.messageId,
      })
      .populate('senderId');

    const newMessages = message.map((mess) => {
      return { ...mess, seen: true };
    });

    console.log(newMessages);
    res.status(200).json(newMessages);
  } catch (error) {
    res.status(500).json({ msg: error.message });
  }
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Mongoose objects are pretty much immutable, you can't really do that.

The solution is to use .lean(), which will make Mongoose return simple JSON data, instead of a Mongoose objects array.

Bonus : it's faster.

const message = await messageModel
      .find({
        conversationId: req.params.messageId,
      })
      .populate('senderId')
      .lean();
about 4 years ago · Juan Pablo Isaza 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