在我的Web应用程序中,用户可以更新其个人资料。
但是在更新配置文件页面上的某些字段中,如果用户未向其添加任何信息,则保存后,这些字段将被删除,就像它们没有数据一样被更新。
但是其他都很好。
这是我的更新帕格文件:
form(action="/account" method="POST" enctype="multipart/form-data")
label(for="name") Name
input(type="text" name="name" value=user.name)
label(for="email") Email Address
input(type="email" name="email" value=user.email)
//- Image Upload
label(for="photo") Photo
input(type="file" name="photo" id="photo" accept="image/gif, image/png, image/jpeg")
if user.photo
img(src=`/uploads/${user.photo}`, alt=user.name width=200)
label(for="neighborhood") Neighborhood
input(type="text" id="neighborhoodUpdate" name="location[vicinity]" value=(user.location && user.location.vicinity))
失败的领域是照片之一,附近的(邻里)领域。
那么我有这条路线:
router.post('/account',
userController.upload,
catchErrors(userController.resize),
catchErrors(userController.updateAccount)
);
在我的控制器中,我有以下方法:
const multerOptions = {
storage: multer.memoryStorage(),
fileFilter(req, file, next) {
const isPhoto = file.mimetype.startsWith('image/');
if (isPhoto) {
next(null, true);
} else {
next({ message: 'That filetype isn\'t allowed!' }, false);
}
}
};
// To upload photos
exports.upload = multer(multerOptions).single('photo');
exports.resize = async (req, res, next) => {
// check if there is no new file to resize
if (!req.file) {
next(); // skip to the next middleware
return;
}
const extension = req.file.mimetype.split('/')[1];
req.body.photo = `${uuid.v4()}.${extension}`;
// now we resize
const photo = await jimp.read(req.file.buffer);
await photo.resize(800, jimp.AUTO);
await photo.write(`./public/uploads/${req.body.photo}`);
// once we have written the photo to our filesystem, keep going!
next();
};
最后更新帐户:
exports.updateAccount = async (req, res) => {
req.body.location.type = 'Point';
const updates = {
name: req.body.name,
photo: req.body.photo,
email: req.body.email,
musicLink: req.body.musicLink,
genres: req.body.genres,
location: {
vicinity: req.body.vicinity,
address: req.body.location.address,
type: req.body.location.type,
coordinates: [
req.body.location.coordinates[0],
req.body.location.coordinates[1],
]
}
};
const user = await User.findOneAndUpdate(
{ _id: req.user._id },
{ $set: updates },
{ new: true, runValidators: true, context: 'query' }
);
req.flash('success', 'Updated the profile!');
res.redirect('back');
};
据我所见,照片和附近区域不应不使用任何数据进行更新,但是如果用户不添加新数据,则不知何故,旧数据将被删除。
有什么想法吗?
哦,这是我的用户模型:
const userSchema = new Schema({
email: {
type: String,
unique: true,
lowercase: true,
trim: true,
validate: [validator.isEmail, 'Invalid Email Address'],
required: 'Please Supply an email address'
},
name: {
type: String,
required: 'Please supply a name',
trim: true
},
resetPasswordToken: String,
resetPasswordExpires: Date,
props: [
{ type: mongoose.Schema.ObjectId, ref: 'User' }
],
// New stuff
slug: {
type: String,
unique: true,
},
genres: [String],
musicLink: String,
created: {
type: Date,
default: Date.now
},
location: {
type: {
type: String,
default: 'Point'
},
coordinates: [{
type: Number,
required: 'You must supply coordinates!'
}],
address: {
type: String,
required: 'You must supply an address!'
},
vicinity: {
type: String,
},
},
photo: String,
});
答案 0 :(得分:1)
我认为,如果这些字段的定义不足,则可能需要从更新对象中明确删除它们,否则这些字段将被设置为undefined。当使用thr $ set运算符时,更新对象中仅存在键意味着对目标记录中的键进行了更改。
请执行以下操作:
如果(!updates.photo)删除updates.photo