从表单获取用户输入并通过 id - mongodb 和 node.js 更新它

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

如何使用 app.post 在服务器中通过硬编码 MLAB ID 来更新记录。

任何帮助都会很棒。

谢谢

已回答

尝试更新您的记录而不是插入新记录。要更新您的记录,您的记录必须包含一个不可变字段。

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

试试这个:

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()
    }
  })
});

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