我是新手,正在为我的论文工作。 我能够创建插入和删除功能,但在更新数据时遇到问题。
以下是controllers文件夹下的代码:
插入文档:
module.exports.register = (params) => {
let user = new User({
firstName: params.firstName,
lastName: params.lastName,
department: params.department,
position: params.position,
email: params.email,
mobileNo: params.mobileNo,
password: bcrypt.hashSync(params.password, 10),
isAdmin: params.isAdmin,
departments: {
departmentId: params.departmentId
}
})
return user.save().then((user, err) => {
return (err) ? false : true
})
}
删除文档:
module.exports.deleteUser = (params) => {
return User.findByIdAndRemove(params.userId).then((user, err) => {
return (err) ? false : true
})
}
虽然下面是路由器的代码:
插入文档:
router.post('/register', (req, res) => {
UserController.register(req.body).then(result => res.send(result))
});
删除文档:
router.delete('/delete/:id', (req, res) => {
let userId = req.params.id
UserController.deleteUser({userId}).then(user => res.send(user))
});
我到目前为止尝试的是使用删除逻辑,而不是 findOneAndRemove,我使用了 findOneAndUpdate 但它没有更新数据。它只是发送真正的值,而不是更新文档。
我也尝试过 YouTube 中的一些逻辑和这里的一些逻辑,但它与我们构建数据的方式不匹配,因此我很难理解它们。
目标是使用用户的 id 更新寄存器中的相同值。
我正在尝试使用此代码进行更新:
控制器:
module.exports.updateUser = (params) => {
return User.findOneAndUpdate(params.userId, (user, err) => {
return (err) ? false : true
})
}
路线:
router.post('/update/:id', (req, res) => {
UserController.updateUser(req.params.id).then(user => res.send(user))
});
我也尝试添加参数,但它不起作用。我要更新的是整个细节示例:
firstName: params.firstName,
lastName: params.lastName,
department: params.department,
position: params.position,
email: params.email,
mobileNo: params.mobileNo,
password: bcrypt.hashSync(params.password, 10),
isAdmin: params.isAdmin,
departments: {
departmentId: params.departmentId
}
示例用户:
{
"userId": "60f649bd8896c80004b3ffbe",
"firstName": "Jane",
"lastName": "Joe",
"department": "Accounting",
"position": "head",
"email": "janedoe@mail.com",
"mobileNo": "0",
"password": "pass123",
"isAdmin": "yes",
"departments": {
"departmentId": "60efcbec769cf60004b85319"
}
}
这是我的更新代码: 控制器:
module.exports.updateUserData = (params) => {
const dataToUpdate = {
firstName: params.firstName,
lastName: params.lastName,
department: params.department,
position: params.position,
email: params.email,
mobileNo: params.mobileNo,
password: bcrypt.hash(params.password, 10),
isAdmin: params.isAdmin,
departments: {
departmentId: params.departmentId
}
}
User.findOneAndUpdate({userId:params.userId}, {$set:dataToUpdate}, {new: true}, (err, doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
console.log(doc);
});
}
路由器:
router.post('/update-user/:id', (req, res) => {
let userId = req.params.id;
UserController.updateUserData({userId}).then(doc => res.send(doc))
})
使用此代码我收到错误消息:TypeError:无法读取路由器文件夹下未定义的属性“then”。
答案 0 :(得分:0)
您应该尝试跟随。它可能满足您的要求
const dataToUpdate = {
firstName: params.firstName,
lastName: params.lastName,
department: params.department,
position: params.position,
email: params.email,
mobileNo: params.mobileNo,
password: bcrypt.hashSync(params.password, 10),
isAdmin: params.isAdmin,
departments: {
departmentId: params.departmentId
}
}
User.findOneAndUpdate({userId:params.userId}, {$set:dataToUpdate}, {new: true}, (err, doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
console.log(doc);
});