簡體   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