0

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

Any help would be great.

Thanks

Answered

2 Answers2

0

Try updating your record instead of inserting a new one. To update your record your record must contain an immutable field.

Refer this question to see how to find and update with mongoose

ashutosh singh
  • 511
  • 3
  • 15
0

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

Akashdeep Singh
  • 388
  • 4
  • 14
  • @stack My bad, I should've explained. I've updated the code. Replace your /form1 handler with this. This **should** work. – Akashdeep Singh Mar 18 '19 at 02:07
  • Hey @stack, that's a different problem. Have a look at https://stackoverflow.com/q/32368096/2691993 , https://stackoverflow.com/q/34158112/2691993 and https://stackoverflow.com/q/48150072/2691993 . I suggest you read a little about transferring data from server to client. If you're stuck, you can always create a new question with your specific problems. Hope that helps. – Akashdeep Singh Mar 18 '19 at 14:34