Saving an array property on a Mongoose schema

后端 未结 2 1548
温柔的废话
温柔的废话 2020-12-29 14:48

I have a mongoose object schema that looks similar to the following:

var postSchema = new Schema({
   imagePost: {
     images: [{
        url: String,
              


        
2条回答
  •  梦谈多话
    2020-12-29 15:14

    You're missing the imagePost object of your schema in your new object. Try this instead:

    var new_post = new Post();
    new_post.imagePost = { images: [] };
    for (var i in req.body.post_content.images) {
      var image = req.body.post_content.images[i];
      var imageObj = { url: image['url'], text: image['text'] };
      new_post.imagePost.images.push(imageObj);
    }
    new_post.save();
    

提交回复
热议问题