首页 > 解决方案 > 猫鼬 findByIdAndUpdate 我完全不明白

问题描述

嗨,我遇到了问题findByIdAndUpdate。我做错了什么?

router.post('/add/:id', function(req, res) {
  const body = req.body;
  const task = NewTask(body);
  console.log(task);
  console.log(task.id);
  const updateObj = {
    description: task.description,
    selectValue: task.selectValue,
    timeToDo: task.timeToDo
  };
  console.log('Object:', updateObj);
  NewTask.findByIdAndUpdate(task.id, updateObj, { new: true });

  res.redirect('/tasks');
});

标签: mongoose

解决方案


抱歉,我认为我们根本不了解对方,但现在可以在您的帮助下工作。那是最后的代码:

router.post('/add/:id', function (req, res) {
const body = req.body;
const id = req.params.id;
const updateObj = {
    description: body.description,
    selectValue: body.selectValue,
    timeToDo: body.timeToDo
};

console.log('Object:', updateObj);
NewTask.findByIdAndUpdate(id, updateObj, { new: true }, (err, doc) => {
    if (err) {
        console.log('There was an error updating task');
    } else {
        res.redirect('/tasks');
    }
});

});


推荐阅读