gpt4 book ai didi

javascript - 缺少必需参数 - public_id,以及为 Cloudinary 分配给 imageId 的内容

转载 作者:太空宇宙 更新时间:2023-11-04 01:26:15 27 4
gpt4 key购买 nike

我正在建立一个网站,用户可以上传对阅读小说的见解和评论,用户可以自由地获取或不获取小说的图像。

如果选择图像,则帖子模式具有用于 Cloudinary 上传和 future 操作的 imageimageId 属性,例如从 Cloudinary 库更改(更新路由)或删除(销毁路由)它。如果未选择图像,则会出现默认图像。

问题是,我设法使 default image 功能正常工作,但是我不想将多个相同的默认图像上传到 Cloudinary 库,因此我将 default_image.jpg 放在本地服务器文件夹 (/public/images) 中,准确地说,这个 default_image.jpg 不应该在 Cloudinary 库中,这应该节省大量容量。

但是,对于这些没有选择图像的帖子,我不知道应该为其 imageId 属性分配什么。

我尝试了 undefinednull,当然,它们不起作用,因为这样,如果它们都未定义或为 null,它将无法找到某些 novel.imageId

// Schema for data
var fictionSchema = new mongoose.Schema({
...
...
image: String,
// for cloudinary API to track which image to delete or update
imageId: String,
...
...
});
// CREATE route
router.post("/", middleware.isLoggedIn, upload.single('image'), (req, res) => {
if (req.file){
// req.file.path comes from multer npm
cloudinary.v2.uploader.upload(req.file.path, function (err, result) {
if (err) {
...
}
// add cloudinary url for the image to the novel object under image property
req.body.novel.image = result.secure_url;
req.body.novel.imageId = result.public_id;
});
} else {
// setup default image for new novels
req.body.novel.image = "/images/noimage.jpg";

// imageId here should be ... empty? Undefined? Null? A fixed ID but may be delete when accessing Destroy route?
req.body.novel.imageId = undefined;
}
...
...
Novel.create(req.body.novel, function (err, newlyAddedNovel) {
if (err) {
req.flash('error', err.message);
...
} else {
req.flash("success", "Novel added successfully.");
...
}
});
});
// DESTROY route
router.delete("/:id", middleware.checkNovelOwnership, (req, res) => {
// find and delete the correct novel along with the image on cloudinary
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
req.flash("error", err.message);
return res.redirect("back");
}
try {
await cloudinary.v2.uploader.destroy(novel.imageId);
// delete the novel found
novel.remove();
// delete the image from cloudinary
req.flash("success", "Novel deleted successfully.");
...
...
} catch (err) {
..
}
});
});

最佳答案

好消息,我解决了这个问题!她——雷!不过花了我大约2个小时...

所以,最终一切都按照我想要的方式进行,如此巧妙。

代码如下:

• 创建路线

对于新帖子,我首先使用默认图像设置每本新小说,然后如果提供了图像(req.file),则更改它并上传。

然后,记得在 mongo 数据库中创建新数据。 (在我的例子中是Novel.create()。)

router.post("/", middleware.isLoggedIn, upload.single('image'), async function (req, res) {
// set every new novel with default image first
req.body.novel.image = "https://res.cloudinary.com/dvfkbz6la/image/upload/v1565434656/noimage_ew1uri.jpg";
req.body.novel.imageId = "noimage_ew1uri";
req.body.novel.user = {
id: req.user._id,
username: req.user.username
};

if (req.file) {
// req.file.path comes from multer npm
await cloudinary.v2.uploader.upload(req.file.path, function (err, result) {
if (err) {
req.flash('error', err.message);
return res.redirect('back');
}
// add cloudinary url for the image to the novel object under image property
// add image's public_id (imageId in novel model) to novel object
req.body.novel.image = result.secure_url;
req.body.novel.imageId = result.public_id;
});
}
Novel.create(req.body.novel, function (err, newlyAddedNovel) {
if (err) {
...
} else {
...
}
});
});

• 更新路线

在try block 中添加if (novel.imageId = "DEFAULT_IMAGEID") { } else { }。

// UPDATE route
router.put("/:id", middleware.checkNovelOwnership, upload.single('image'), (req, res) => {
// find the correct novel
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
...
} else {
// if there's a req.file, we know user is trying to upload a new image
if (req.file) {
try {
// if imageId is default, await the result to be uploaded
if (novel.imageId = "noimage_ew1uri") {
var result = await cloudinary.v2.uploader.upload(req.file.path);
} else {
// if not default, find the old image using imageId and delete
await cloudinary.v2.uploader.destroy(novel.imageId);
var result = await cloudinary.v2.uploader.upload(req.file.path);
}
novel.imageId = result.public_id;
novel.image = result.secure_url;
} catch (err) {
...
}
}
novel.title = req.body.title;
novel.author = req.body.author;
novel.price = req.body.price;
novel.description = req.body.description;

// remember to save the changed novel
novel.save();
req.flash("success", "Successfully Updated!");
res.redirect("/novels/" + novel._id);
}
});
});

• 摧毁路线

在try block 中添加if (novel.imageId != "DEFAULT_IMAGEID") { } else { }。

// DESTROY route
router.delete("/:id", middleware.checkNovelOwnership, (req, res) => {
// find and delete the correct novel along with the image on cloudinary
Novel.findById(req.params.id, async (err, novel) => {
if (err) {
req.flash("error", err.message);
return res.redirect("back");
}
try {
// if novel.imageId isn't default, destroy it from Cloudinary
if (novel.imageId != "noimage_ew1uri") {
await cloudinary.v2.uploader.destroy(novel.imageId);
}
// delete the novel found
novel.remove();
...
} catch (err) {
...
}
});
});
<小时/>

剩下的唯一问题是,我不知道为什么,但是当点击更新路线时,当 imageId 不是默认的并且用户更改了图像时,

旧镜像不会被销毁并保留在 Cloudinary 库中。真是奇怪。

我确实设置了此代码

Novel.findById(req.params.id, async (err, novel) => {
if (err) {
...
} else {
if (req.file) {
try {
// if imageId is default, await the result to be uploaded
if (novel.imageId = "noimage_ew1uri") {
...
} else {
// if not default, find the old image using imageId and delete
await cloudinary.v2.uploader.destroy(novel.imageId);
var result = await cloudinary.v2.uploader.upload(req.file.path);
}
...
...

不知道为什么等待 cloudinary.v2.uploader.destroy(novel.imageId);没有按预期工作。嗯...

关于javascript - 缺少必需参数 - public_id,以及为 Cloudinary 分配给 imageId 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57439260/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com