【发布时间】:2019-01-28 21:06:14
【问题描述】:
我是一个初学者,试图将这个 json 解析为猫鼬模型,以便我可以保存数据。
这是我现在的模特
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。 谢谢!!!!
【问题讨论】:
-
请提供有关该问题的更多信息。究竟是什么不工作?您是否收到任何错误消息?
标签: javascript node.js express mongoose model