我正在尝试获取用户登录时更新其个人资料的功能。但是,当我尝试更新字段(例如“名称”字段)时,尽管它运行无误,但它没有更新。即使正常运行,它也保持不变。 这是我的代码。
const filterObj = (obj, ...allowedFields) => {
const newObj = [];
Object.keys(obj).forEach((el) => {
if (allowedFields.includes(el)) newObj[el] = obj[el];
});
return newObj;
};
exports.updateMe = catchAsync(async (req, res, next) => {
if (req.body.password || req.body.passwordConfirm) {
return next(new AppError('This route is not for password updates.', 400));
}
//Filtering out field names that are not allowed to be updated
const filteredBody = filterObj(req.body, 'name', 'email');
//Updating the user data
const updatedUser = await User.findByIdAndUpdate(req.user.id, filteredBody, {
new: true,
runValidators: true,
});
res.status(200).json({
status: 'success',
data: {
user: updatedUser,
},
});
});