mongoose - 猫鼬 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');
});
解决方案
抱歉,我认为我们根本不了解对方,但现在可以在您的帮助下工作。那是最后的代码:
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');
}
});
});
推荐阅读
- java - 为什么我的 Java Vertx 项目没有使用 SockJS 连接到 Web 项目?
- javascript - 每个“。”之后的换行符
- javascript - 是否可以拥有独立于前端应用程序的独立赛普拉斯测试存储库?
- asp.net-mvc - MVC 匹配对象到 API 期望
- python - Pycryptodome: ImportError: DLL load failed while importing _AES: %1 is not an valid Win32 application
- python - python产品迭代链内存不足
- javascript - 组件无法从 Vue 实例调用方法 v-on:click
- python - Python以给定的索引跳转或更少的方式递归地跳过列表,同时避免零
- c - 选择排序:什么是n-1?
- c++ - boost::interprocess 消息队列未删除