Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!