Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更新Mongoose模型阵列?_Javascript_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Javascript 如何更新Mongoose模型阵列?

Javascript 如何更新Mongoose模型阵列?,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我对编程相当陌生,所以如果这个问题有一个非常简单的答案,我很抱歉 我正在尝试创建一个简单的网站,用户可以上传多张图片,并将这些图片显示在网站的其他地方。我正在用Mongoose、Express和Node完成这项工作。我使用Multer作为上传中间件,我还使用body解析器,我听说与Multer一起使用时可能会导致复杂性。我正在使用Cloudinary API上传和托管上传的图像 理想情况下,用户将选择要上传的图像,这些图像将上传到Cloudinary,然后每个图像的直接链接将保存在Mongoos

我对编程相当陌生,所以如果这个问题有一个非常简单的答案,我很抱歉

我正在尝试创建一个简单的网站,用户可以上传多张图片,并将这些图片显示在网站的其他地方。我正在用Mongoose、Express和Node完成这项工作。我使用Multer作为上传中间件,我还使用body解析器,我听说与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);
        });
    }
下面是我收到的错误:

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>

错误是因为在
req.body.campground.image
中没有可用的
image
属性,即
undefined
,您试图
推到未定义的数组上,而不是下一行中的数组上

req.body.campground.image.push(newImage[i-1]);
请尝试以下操作:

req.body.campground.image = req.body.campground.image || [];
req.body.campground.image.push(newImage[i-1]);

如果属性存在,那么well和good将为其分配一个空数组

谢谢大家!!这似乎是我一直在寻找的答案
console.log(req.body)
现在返回带有image属性的campgrand对象,并且我没有收到任何错误。现在我尝试显示已上载的图像,它表示数组仍然为空。你认为你可以为这个项目查看我的github吗?@JordanPisani当然可以与我分享,我会看一看。
req.body.campground.image.push(newImage[i-1]);
req.body.campground.image = req.body.campground.image || [];
req.body.campground.image.push(newImage[i-1]);