简体   繁体   English

在博客文章的评论中评论

[英]comment in comment for blog post

I'm using the Mean Stack and trying to get a comment in comment feature to work for a blog post.我正在使用 Mean Stack 并尝试在评论功能中获得评论以用于博客文章。

Where i'm getting stuck is trying to get from angular to mongo to update a comment for a given blog and a comment for a comment.我被卡住的地方是试图从 angular 到 mongo 更新给定博客的评论和评论的评论。 I basically don't know how to get angular/express/mongoose to use the a subdocument id or nested subdocument id to update the parent blog or comment.我基本上不知道如何让 angular/express/mongoose 使用子文档 ID 或嵌套子文档 ID 来更新父博客或评论。

What I have managed to do so far is:到目前为止我设法做的是:

Create my schema -创建我的架构 -

var mongoose = require('mongoose'), Schema = mongoose.Schema;

var childSchema = new Schema;
childSchema.add({ 
firstName: 'string', 
lastName: 'string', 
comment: 'string', 
children: [childSchema] 
});

var parentSchema = new Schema({
firstName: 'string',
lastName: 'string',
blog: 'string',
children: [childSchema]
});

var Parent = mongoose.model('Parent', parentSchema);

Load Mongo with some data -用一些数据加载 Mongo -

{
"firstName": "bobby",
"lastName": "edwards",
"blog": "this is blog 6",
"children": [
                {
            "firstName": "cat",
            "lastName": "edwards",
            "comment": "this is blog 6.1",
            "children": [  
                            {
                        "firstName": "dave",
                        "lastName": "edwards",
                        "comment": "this is blog 6.2",
                        "children": []
                         }]
            }]
}

Get all blogs with nested comments from mongo and display correctly in angular - each blog or comment has a form attached从 mongo 获取所有带有嵌套评论的博客并以角度正确显示 - 每个博客或评论都附有一个表单

Return a single parent blog -返回单亲博客 -

Create a parent blog -创建一个父博客 -

UPDATED更新

I have managed to get one level down but for this i had to send the comment id as a param, modify the express route and pass the results of findbyid to results.comment.id({_id: req.params.comment_id}).我已经设法降低了一级,但为此我必须将评论 ID 作为参数发送,修改快速路由并将 findbyid 的结果传递给 results.comment.id({_id: req.params.comment_id})。

Node App节点应用

  app.get('/parent/:post_id/:comment_id', postsController.single);

Node Controller节点控制器

  module.exports.single = function (req, res) {
  console.log(req.params.post_id);
  Parent.findById({ _id: req.params.post_id }, function (err, results) {
      if (err) {
         console.log(err);
      }
  var id = results.children.id({ _id: req.params.comment_id }); //function (err, commentresults) {

     console.log('triggered 2');
     console.log(id);
     res.json([id]);
  });
};

That being said this was for learning/testing and can't see how this would work if i needed to create/return/update a comment 10 levels down.话虽如此,这是为了学习/测试,如果我需要创建/返回/更新 10 级以下的评论,则无法看到这将如何工作。

Looking at this from a different perspective, I'd just have two optional fields, topLevelCommentId and commentOwnerId , in your schema.从不同的角度来看,我在您的架构中只有两个可选字段topLevelCommentIdcommentOwnerId

Anytime you insert a comment, then, as long as you appropriately set the topLevelCommentId as the top level parent, and commentOwnerId as the immediate parent, you should have no problem getting your comments nested appropriately.每当您插入评论时,只要您适当地将 topLevelCommentId 设置为顶级父级,并将 commentOwnerId 设置为直接父级,您就可以正确地嵌套评论。

Thanks for the feedback.感谢您的反馈。 I think I need to gain a better understanding of what is possible in MongoDB.我想我需要更好地了解 MongoDB 中的可能性。 Therefore I have decided to sit a couple of MongoDB University course.因此,我决定参加一些 MongoDB 大学课程。 One on the Development Side and one on the Operation Side.一个在开发端,一个在运营端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM