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
Arrays 如何让mongoose将URL数组推送到数组子文档_Arrays_Node.js_Mongoose_Mongoose Schema - Fatal编程技术网

Arrays 如何让mongoose将URL数组推送到数组子文档

Arrays 如何让mongoose将URL数组推送到数组子文档,arrays,node.js,mongoose,mongoose-schema,Arrays,Node.js,Mongoose,Mongoose Schema,我正在做一个博客系统与多个上传的图像,和多篇文章。 我已经创建了一个上传屏幕,允许我选择一些以前的图片,然后将其发布到后端。 这一切都正常工作(多亏了我在堆栈溢出时得到的一些帮助),控制台从服务器上记录了这一点: [ 'http://res.cloudinary.com/jacobsiler-com/image/upload/v1574344215/SilerGuitars/f8q5d4kedss1tpmhxmwg.jpg', 'http://res.cloudinary.com/jacob

我正在做一个博客系统与多个上传的图像,和多篇文章。 我已经创建了一个上传屏幕,允许我选择一些以前的图片,然后将其发布到后端。 这一切都正常工作(多亏了我在堆栈溢出时得到的一些帮助),控制台从服务器上记录了这一点:

[ 'http://res.cloudinary.com/jacobsiler-com/image/upload/v1574344215/SilerGuitars/f8q5d4kedss1tpmhxmwg.jpg',
  'http://res.cloudinary.com/jacobsiler-com/image/upload/v1574344227/SilerGuitars/fveajqk0ehwy5mxywysa.jpg',
  'http://res.cloudinary.com/jacobsiler-com/image/upload/v1574344201/SilerGuitars/lfxwkq8xhhkyxn85oyna.jpg' ]
这些是上传到Cloudinary并保存在mongoDB文档中的图像的图像URL。 现在,我尝试使用findOneAndUpdate将此输出保存到所选的post文档:

app.post("/post-images", (req, res) => {
  //const post=req
  var postImages = req.body;
  const postID = postImages.shift();
  console.log(postImages);
  Post.findByIdAndUpdate(
    { _id: postID },
    { $push: { imageUrls: { $each: [{ postImages }] } } },
    { lean: true, new: true },
    function(err, foundPost) {
      if (!err) {
        console.log(foundPost);
        res.redirect("/");
      } else {
        console.log("error: " + err);
      }
    }
  );
  //res.redirect("/");
});
我预先设置了要将图像添加到postmages数组的post ID,然后将其分离到post ID const中,并记录字符串数组。这是我选择的ID。然后我尝试将字符串数组中的字符串推送到文档中。 我可以看到,在文档中可能只会以一个字符串结束,我不知道如何正确处理这个问题。我需要以某种方式分离保存的URL

以下是我在Robo 3T中的帖子:

我想要的结果是高亮显示的对象是数组中的一个url,而所有其他类似的对象是一个指向图像的url

我尝试过使用不同的更新函数(updateOne、findbyidanddupdate、findOneAndUpdate等),并将不同的选项传递给它们。我似乎也尝试过这一行中的每一种简洁组合:
{$push:{imageurl:{$each:[{{postmages}]}}}}

一切都没有用。以下是我的模式和模型:

//Defining the Image Schema
const imageSchema = {
  url: String,
  id: String
};

const Image = new mongoose.model("Image", imageSchema);

//Defining the Post Schema
const postSchema = {
  title: String,
  content: String,
  imageUrls: [{ url: String }]
};

const Post = new mongoose.model("Post", postSchema);
我不确定我错过了什么。
非常感谢所有帮助和建议,以使这项工作顺利进行。

通过尝试、出错和查阅猫鼬文档,我最终找到了我想要的答案。如果你有同样的问题,我希望答案能帮助你

首先,我需要改变我定义模式的方式,因为它不是一个对象数组,而是一个数组:

//Defining the Post Schema
const postSchema = {
  title: String,
  content: String,
  imageUrls: [ String ]
};
以前的url和id字段对于我来说是不必要的,所以我也删除了它们。 然后我在mongoose文档中查阅了足够多的内容,偶然发现并阅读了它的功能。这似乎是答案,事实证明确实如此。要将图像URL保存到文档,我使用以下代码结束:

app.post("/post-images", (req, res) => {
  //const post=req
  var postImages = req.body;
  const postID = postImages.shift();
  console.log(postImages);
  Post.updateOne(
    { _id: postID },
    {
      $addToSet: { imageUrls: { $each: postImages } }
    },
    function(err, foundPost) {
      if (!err) {
        console.log(foundPost);
        res.redirect("/");
      } else {
        console.log("error: " + err);
      }
    }
  );
现在,我的代码正确地保存了我的URL数组,每个URL都是
imageURL
下的一个单独字符串