我正在處理我的項目的用戶帳戶部分。我成功地完成了GET,POST和DELETE方法,現在我錯過了PUT。貓鼬更新功能不起作用
當用戶提交更新用戶表單,該req.body
會是這樣的
{ user:
{ firstName: 'asas',
lastName: 'assa',
studentId: '1234',
email: '[email protected]',
password: '123'
}
}
我的模式是這樣的:
const userSchema = new Schema({
cuid: { type: 'String', required: true },
firstName: { type: 'String', required: true },
lastName: { type: 'String', required: true },
studentId: { type: 'Number', required: true },
password: { type: 'String', required: true },
email: { type: 'String', required: true },
dateAdded: { type: 'Date', default: Date.now, required: true },
lastLogin: { type: 'Date', default: null, required: false },
});
最後我更新功能看起來是這樣的。
export function updateUser(req, res) {
console.log(req.body)
firstName = sanitizeHtml(req.body.user.firstName);
lastName = sanitizeHtml(req.body.user.lastName);
studentId = sanitizeHtml(req.body.user.studentId);
email = sanitizeHtml(req.body.user.email);
password = sha512(req.body.user.password).toString('hex');
let update = { firstName, lastName, studentId, email, password };
User.findOneAndUpdate(req.params.cuid,update,function(err,updated){
if(error){
return res.status(500).send(err);
}else{
return res.json({ user: updated });
}
});
}
我不明白爲什麼我的put方法不工作,也許第二雙眼睛可以看到缺陷。
這是適合你嗎? –