Search code examples
node.jsmongodbexpressmlab

Get user input from form and update it by id - mongodb and node.js


How to update a record by hard coding the MLAB ID in the server using app.post.

Any help would be great.

Thanks

Answered


Solution

  • Try this:

    app.post('/form1', function (req, res, next) {
      record.findOneAndUpdate({"_id": ObjectId("THE ID IN MLAB")}, req.body, {new: true}, function (err, doc) {
        if (err) {
          res.status(500).end()
        } else {
          res.status(200).end()
        }
      })
    });
    

    Documentation: https://mongoosejs.com/docs/api.html#model_Model.findOneAndUpdate