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 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

@stack My bad, I should've explained. I've updated the code. Replace your /form1 handler with this. This should work.
Hey @stack, that's a different problem. Have a look at stackoverflow.com/q/32368096/2691993 , stackoverflow.com/q/34158112/2691993 and 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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.