首先发布路线,图像保存成功:
router.post("/agent", upload.single('image'), function (req, res) {
cloudinary.uploader.upload(req.file.path, function (result) {
var name = req.body.name;
var image = result.secure_url;
var imageId = result.public_id;
var newReview = { name: name, imageId: imageId, image: image };
//create a new campground and save to DB
Agent.create(newReview, function (err, newlyCreated) {
if (err) {
console.log(err);
} else {
res.redirect('/agent/' + newlyCreated.id);
}
});
});
});
然后,我正在尝试通过放置路线编辑图像,但是我遇到了2个问题: 1.错误消息:缺少必需的参数-public_id 2.'name'字段将从数据库中删除。 这是放置(编辑)路线:
router.put("/agent/:id", upload.single('image'), function (req, res) {
Agent.findById(req.params.id, req.body.agent, async function (err, updatedReview) {
if (err) {
res.redirect("/agent");
} else {
if (req.file) {
try {
await cloudinary.v2.uploader.destroy(updatedReview.imageId);
var result = await cloudinary.uploader.upload(req.file.path);
updatedReview.agent.imageId = result.public_id;
updatedReview.agent.image = result.secure_url;
} catch (err) {
req.flash("error", err.message);
return res.redirect("/agent");
}
}
updatedReview.name = req.body.name;
updatedReview.save();
req.flash("success");
res.redirect("/agent/" + req.params.id);
}
});
});
假设html(ejs)文件中的名称标签和enctype都是正确的,有人可以指出我出了什么问题吗? 谢谢!