javascript - 如何更新 Mongoose 模型阵列?
问题描述
我对编程相当陌生,所以如果这个问题有一个非常简单的答案,我深表歉意。
我正在尝试创建一个简单的网站,用户可以在其中上传多个图像并将这些图像显示在网站的其他位置。我正在使用 Mongoose、Express 和 Node.js 完成此任务。我使用 Multer 作为上传中间件,并且我还使用身体解析器,我听说与 Multer 一起使用会导致并发症。我正在使用 Cloudinary API 上传和托管上传的图像。
理想情况下,用户将选择他们想要上传的图像,这些图像将被上传到 Cloudinary,然后每个图像的直接链接将保存在该特定帖子的 Mongoose 模型中,然后使用图像链接显示每个图像已被保存。
到目前为止,除了一个问题外,我的一切都运行良好。我遇到的问题是,当我尝试将上传图像的提供链接推送到 Mongoose 模型数组时,我收到一个错误,我无法找到解决方法。
这是上传图像并将其推送到数组中的代码:
var imageLength;
var newImage = new Array();
router.post("/", isLoggedIn, upload.array("image"),function(req, res){
image: [];
imageLength = req.files.length;
for(var i = 0; i < imageLength; i++){
cloudinary.uploader.upload(req.files[i].path, function(result) {
// add cloudinary url for the image to the campground object under image property
newImage.push(result.secure_url);
console.log(newImage[i-1]);
console.log(req.body);
req.body.campground.image.push(newImage[i-1]);
console.log(req.body);
});
}
这是“露营地”的猫鼬模型:
var mongoose = require("mongoose");
var campgroundSchema = new mongoose.Schema({
name: String,
image: [String],
image_id: [String],
description: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
},
comments: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Comment"
}
]
});
module.exports = mongoose.model("Campground", campgroundSchema);
这是我收到的错误:
https://res.cloudinary.com/jpisani/image/upload/v1525280683/r1cs0zmjrznopot7lmu9.jpg
{ campground:
{ name: 'g',
description: 'g',
author: { id: 5aaaf25986f338289129f8ea, username: 'frff' } } }
/home/ubuntu/workspace/YelpCamp/v10/routes/campgrounds.js:50
req.body.campground.image.push(newImage[i-1]);
^
TypeError: Cannot read property 'push' of undefined
如您所见,图像已成功上传到 Cloudinary,我有该图像的直接链接。问题在于console.log(req.body);
。没有列出阻止我将链接推入图像数组的图像属性。
我知道这req.body
仅包含用户提交的内容,但我还没有找到任何其他方法解决此问题的方法。
这是创建新帖子页面的代码:
<div class="row">
<h1 style="text-align: center">Create a New Campground</h1>
<div style="width: 30%; margin: 25px auto;">
<form action="/campgrounds" method="POST" enctype="multipart/form-data">
<div class="form-group">
<input class="form-control" type="text" name="campground[name]" placeholder="name">
</div>
<div class="form-group">
<label for="image">Image</label>
<input type="file" id="image" name="image" accept="image/*" multiple required>
</div>
<div class="form-group">
<input class="form-control" type="text" name="campground[description]" placeholder="description">
</div>
<div class="form-group">
<button class="btn btn-lg btn-primary btn-block">Submit!</button>
</div>
</form>
<a href="/campgrounds">Go Back</a>
</div>
</div>
如您所见,此代码的图像上传部分(位于中心)被命名为“图像”,这应该使 Mongoose 模型中的图像数组在我出现时出现,console.log(req.body);
但它似乎没有这样做。
如果需要任何信息,请询问,我会及时回复。任何帮助将不胜感激。提前致谢。
编辑:已找到解决方案!对于将来遇到此问题的任何人,这里是问题的答案。
//create - add new campground to DB
router.post("/", isLoggedIn, upload.array("campground[image]"), async function(req, res){
// add author to campground
req.body.campground.author = {
id: req.user._id,
username: req.user.username
};
req.body.campground.image = [];
for (const file of req.files) {
let result = await cloudinary.uploader.upload(file.path);
req.body.campground.image.push(result.secure_url);
}
Campground.create(req.body.campground, function(err, campground) {
if (err) {
return res.redirect('back');
}
res.redirect('/campgrounds/' + campground.id);
});
});
解决方案
该错误是因为您req.body.campground.image
没有image
可用的属性,也就是说undefined
,您试图push
在下一行中使用未定义而不是数组
req.body.campground.image.push(newImage[i-1]);
尝试以下操作:
req.body.campground.image = req.body.campground.image || [];
req.body.campground.image.push(newImage[i-1]);
如果该属性存在,则很好,否则为它分配一个空数组。
推荐阅读
- php - 我无法访问 PHP 中的页面
- python - VSCode 中的 Intellisense 和 Python 类型提示
- linux - 如何使用 bash 命令行重命名以序列开头的文件
- mongodb - 如何在 Mongoose 的聚合查询中访问字段值
- python - 跳出 For 循环和 If 语句
- r - 一个一个地绘制二进制向量的条件组合
- kotlin - kotlin:如何制作可滑动的堆栈卡视图?
- ffmpeg - 在 vlc 上接收像素化和滞后的 rtsp 流时
- bash - 在 bash 脚本中包含源代码时,如何修复或屏蔽使用 shellcheck 发现的 1091 错误?
- json - 尝试将 Excel 格式转换为 JSON