///我想制作一个可以在其中编辑图像数组的api。例如,我为特定字段上传了5张图像,然后我想要编辑该图像,例如从5张图像中删除3张图像并添加3张新图像,而前2张应保持不变。
///我尝试了传统方法来更新图像,但是所有图像都被覆盖。因此,之前的5张图片将被删除并获得新的3张图片,但是我希望保留新的3张图片和之前的2张图片。
//添加数据的功能。
const setData = (req, res) => {
console.log(req.body);
if (!req.files) res.send({ success: 0, msg: "Please upload a valid image" });
const newProperty = {
userid: req.body.userid,
address: req.body.address,
price: req.body.price,
bedroom: req.body.bedroom,
bathroom: req.body.bathroom,
livingroom: req.body.livingroom,
propertytype: req.body.propertytype,
propertynumber: req.body.propertynumber,
streetname: req.body.streetname,
city: req.body.city,
images: req.files.images,
floorPlanImages: req.files.floorPlanImages,
epcImages: req.files.epcImages,
country: req.body.country,
postalcode: req.body.postalcode,
description: req.body.description
};
Property.create(newProperty, (err, property) => {
if (err) sendError(req, res, err);
else {
sendData(req, res, property);
}
});
};
//以未决状态将属性存储在数据库中。
router.post(
"/api/addProperty",
upload.fields([
{
name: "images",
maxCount: 10
},
{
name: "floorPlanImages",
maxCount: 10
},
{
name: "epcImages",
maxCount: 10
}
]),
(req, res) => {
setData(req, res);
}
);
//用于编辑数据的功能。
const editData = (req, res, id) => {
if (!req.files) res.send({ success: 0, msg: "Please upload a vaalid image" });
Property.findOne({ _id: id }).then(Property => {
Property.address = req.body.address;
Property.price = req.body.price;
Property.bedroom = req.body.bedroom;
Property.bathroom = req.body.bathroom;
Property.livingroom = req.body.livingroom;
Property.propertytype = req.body.propertytype;
Property.propertynumber = req.body.propertynumber;
Property.streetname = req.body.streetname;
Property.city = req.body.city;
property.images = req.files.images;
property.floorPlanImages = req.files.floorPlanImages;
property.epcImages = req.files.epcImages;
Property.country = req.body.country;
Property.postalcode = req.body.postalcode;
Property.description = req.body.description;
Property.save()
.then(updated => {
res.send(200).json(updated);
})
.catch(err => {
res.send(err);
});
});
};
//编辑属性表单
router.post(
"/api/editProperty",
upload.fields([
{
name: "images",
maxCount: 10
},
{
name: "floorPlanImages",
maxCount: 10
},
{
name: "epcImages",
maxCount: 10
}
]),
(req, res) => {
editData(req, res, req.body.id);
}
);
So , i want to to edit the images in such a way that if there is 5 images and i remove 3 image and add 3 new , the previous 2 should stay there.