Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/364.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 如何从这个JSON构建mongoose模型_Javascript_Node.js_Express_Mongoose_Model - Fatal编程技术网

Javascript 如何从这个JSON构建mongoose模型

Javascript 如何从这个JSON构建mongoose模型,javascript,node.js,express,mongoose,model,Javascript,Node.js,Express,Mongoose,Model,我是一个初学者,试图将这个json解析成mongoose模型,以便保存数据 这是我现在的模型 const commentSchema = new Schema([{ sectionId: String,comments: [{ id: String, authorAvatarUrl: String, authorName: String, authorId: String, authorUrl: String,

我是一个初学者,试图将这个json解析成mongoose模型,以便保存数据

这是我现在的模型


const commentSchema = new Schema([{

  sectionId: String,comments:
 [{

      id: String,
      authorAvatarUrl: String,
      authorName: String,
      authorId: String,
      authorUrl: String,
      comment: String,

replies:
 [{
        id: String,
        authorAvatarUrl: String,
        authorName: String,
        authorId: String,
        authorUrl: String,
        comment: String,
        parentId: String
      }]
  }]

}]);

const Comment = mongoose.model("Comment", commentSchema);
module.exports = Comment;

这就是我试图映射的Json

var existingComments = [{
    "sectionId": "1",
    "comments":
 [{
        "id": 88,
        "authorAvatarUrl": "support/images/jon_snow.png",
        "authorName": "Jon Sno",
        "authorId": 1,
        "authorUrl": "http://en.wikipedia.org/wiki/Kit_Harington",
        "comment": "I'm Ned Stark's bastard",
        "replies":
 [{
          "id": 100,
          "authorAvatarUrl": "support/images/jon_snow.png",
          "authorName": "Jon Sno",
          "authorId": 1,
          "authorUrl": "http://en.wikipedia.org/wiki/Kit_Harington",
          "comment": "P.S.: I know nothing.",
          "parentId": 88
        }]
    }]
}]

在服务器端,我试图得到这样的评论

//Comment posted
router.post("/comments", (req, res, next) => {
  //Save the comment on database
  const [{
    sectionId,

comments:
 [{
      id,
      authorAvatarUrl,
      authorName,
      authorId,
      authorUrl,
      comment,

      replies: 
[{
        id,
        authorAvatarUrl,
        authorName,
        authorId,
        authorUrl,
        comment,
        parentId
      }]
    }]
  }] = req.body;


  const newComment = new Comment([{
    sectionId,comments:
 [{ id, }]

 }]);

  newComment
    .save()
    .then(comment => {
      res.redirect("/index");

    })

    .catch(err => {
      console.log(err);
    });

});

但它不起作用,任何帮助都将不胜感激,因为我目前正试图更好地理解moongose ODM。
谢谢

您的注释模式有点混乱。您创建了一个“注释”模式(请注意,这是单数),但您试图将注释模式用作注释数组

提示:保持你的模式和单数

为了实现您想要的设计,同时保持您的模型的独特性,您可以尝试以下方法:

解决方案(1):有一个存储所有数据的“节”集合 注意:尽管您有多个架构,但我们只对SectionSchema建模,它将是我们唯一的集合,其中每个Section文档都包含您需要的所有信息

const sectionSchema = new Schema({
    name: {
        type: String,
        required: [true, 'Section name is required']
    },
    comments: {
        type: [commentSchema]
    }
});

const replySchema = new Schema({
    //id: String, //not needed, mongoose will automatically add an "_id" property when you save 
    authorAvatarUrl: String,
    authorName: String,
    authorId: String,
    authorUrl: String,
    comment: String,
    parentId: String
});

const commentSchema = new Schema({
    //id: String, //not needed, mongoose will automatically add an "_id" property when you save 
    authorAvatarUrl: String,
    authorName: String,
    authorId: String,
    authorUrl: String,
    comment: String,
    replies:{
        type: [replySchema]
    }
});

module.exports =  mongoose.model("Section", sectionSchema);
在上述解决方案中,您仍然可以使用路由仅处理特定注释,例如:

router.get("/sections/:id") //gets the entire section document matching the ID
router.post("/sections/:id/comments/") //creates a new comment within section with matching ID. Simply push a new comment into the comments array
router.get("/sections/:id/comments/") //gets all the comments for this section. Simply return only the comments property of the section document.
router.get("/sections/:id/comments/:commentId") //gets a specific comment within a specific section. Filter the comments array to get the comment matching the commentId and then only return that.

etc..

最后一点注意:还有其他方法可以为数据建模。这只是一个例子。例如,您可以有一个节集合和一个注释集合,其中注释文档存储一个节Id,该Id指示该注释文档所涉及的节


查看一下,它可能会帮助您了解数据建模的不同方式,以及mongoose子文档的工作原理和使用方法。

请提供有关该问题的更多信息。到底是什么不起作用?您是否收到任何错误消息?