繁体   English   中英

在博客文章的评论中评论

[英]comment in comment for blog post

我正在使用 Mean Stack 并尝试在评论功能中获得评论以用于博客文章。

我被卡住的地方是试图从 angular 到 mongo 更新给定博客的评论和评论的评论。 我基本上不知道如何让 angular/express/mongoose 使用子文档 ID 或嵌套子文档 ID 来更新父博客或评论。

到目前为止我设法做的是:

创建我的架构 -

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);

用一些数据加载 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": []
                         }]
            }]
}

从 mongo 获取所有带有嵌套评论的博客并以角度正确显示 - 每个博客或评论都附有一个表单

返回单亲博客 -

创建一个父博客 -

更新

我已经设法降低了一级,但为此我必须将评论 ID 作为参数发送,修改快速路由并将 findbyid 的结果传递给 results.comment.id({_id: req.params.comment_id})。

节点应用

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

节点控制器

  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]);
  });
};

话虽如此,这是为了学习/测试,如果我需要创建/返回/更新 10 级以下的评论,则无法看到这将如何工作。

从不同的角度来看,我在您的架构中只有两个可选字段topLevelCommentIdcommentOwnerId

每当您插入评论时,只要您适当地将 topLevelCommentId 设置为顶级父级,并将 commentOwnerId 设置为直接父级,您就可以正确地嵌套评论。

感谢您的反馈。 我想我需要更好地了解 MongoDB 中的可能性。 因此,我决定参加一些 MongoDB 大学课程。 一个在开发端,一个在运营端。

暂无
暂无

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

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