Error when updating record in MongoDB through Node.js

Hi All! I am learning how to fetch data from MongoDB using Express (Node). I am unable to perform updateOne() function as I am getting the error - TypeError: Cannot read property ‘name’ of undefined. Here’s the code snippet:

router.put('/api/v3/app/events/:id', (req, res) => {

  const filter = { _id: new ObjectId(req.params.id) };

  const options = { upsert: true };

  const updatedDoc = {
    $set: {
      name: req.params.body.name, tagline: req.params.body.tagline,
      schedule: req.params.body.schedule, description: req.params.body.description, files: req.params.body.files,
      moderator: req.params.body.moderator, category: req.params.body.category,
      sub_category: req.params.body.sub_category, rigor_rank: parseInt(req.params.body.rigor_rank)
    },
  }

  updateData(filter, updatedDoc, options)
    .then((result) => {
      console.log(${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s))
      res.status(200).end()

    })
    .catch((err) => {
      console.log(err)
      res.status(500).end()
    })
})                                                                                                                                                                                                                                                        const updateData = (filter,body,options) => {
  const collection = db.collection('events')
  const result =  collection.updateOne(filter, body, options)
  return result;
}

Hey :wave: @Geethika_S,

I guess instead of req.params.body.name, you should use req.body.name to access the properties of the request body. The params object is used specifically for extracting route parameters, while the body object is used to access the data sent in the request body.

Here is the updated code snippet for your reference:

// Route handler
app.put('/api/v3/app/events/:id', async (req, res) => {
    try {
        const filter = { _id: new ObjectId(req.params.id) };
        const options = { upsert: true };

        const updatedDoc = {
            $set: {
                name: req.body.name,
                tagline: req.body.tagline,
                schedule: req.body.schedule,
                description: req.body.description,
                files: req.body.files,
                moderator: req.body.moderator,
                category: req.body.category,
                sub_category: req.body.sub_category,
                rigor_rank: parseInt(req.body.rigor_rank)
            },
        };

        const result = await updateData(filter, updatedDoc, options);
        console.log(`${result.matchedCount} document(s) matched the filter, updated ${result.modifiedCount} document(s)`);
        res.status(200).end();
    } catch (err) {
        console.log(err);
        res.status(500).end();
    }
});

const updateData = async (filter, body, options) => {
    const collection = db.collection('events');
    const result = await collection.updateOne(filter, body, options);
    return result;
};

Please let us know if you have any further questions.

Best regards,
Kushagra

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.