Express.js嵌套路由的数据关联问题

Express.js嵌套路由的数据关联问题,express,mongoose,mongoose-schema,mongoose-populate,Express,Mongoose,Mongoose Schema,Mongoose Populate,假设我希望REST端点大致如下所示: /blogs /blogs/new /blogs/:id /blogs/:id/edit /blogs/:id/comments/new 每个if上的积垢都有意义。例如,/blogs POST创建一个新博客,GET获取所有博客/blogs/:id GET只获取一个包含相关评论的博客/blogs/:id/comments/POST为该特定博客创建新评论 现在一切都很好,但与每个博客的评论关联没有正常工作。我认为我的模型或/blogs/:id/comments

假设我希望REST端点大致如下所示:

/blogs
/blogs/new
/blogs/:id
/blogs/:id/edit
/blogs/:id/comments/new
每个if上的积垢都有意义。例如,/blogs POST创建一个新博客,GET获取所有博客/blogs/:id GET只获取一个包含相关评论的博客/blogs/:id/comments/POST为该特定博客创建新评论

现在一切都很好,但与每个博客的评论关联没有正常工作。我认为我的模型或/blogs/:id/comments/newroute会造成这个错误
blogSchema

var blogSchema=new mongoose.Schema({
    title:String,
    image:String,
    body:{type:String, default:""},
    created:{ type: Date },
  comments:[{
    type:mongoose.Schema.Types.ObjectId,
    ref:'Comment'
  }]
});
var commentSchema=mongoose.Schema({
    text:String,
    author:String
})
commentSchema

var blogSchema=new mongoose.Schema({
    title:String,
    image:String,
    body:{type:String, default:""},
    created:{ type: Date },
  comments:[{
    type:mongoose.Schema.Types.ObjectId,
    ref:'Comment'
  }]
});
var commentSchema=mongoose.Schema({
    text:String,
    author:String
})
所有与评论相关的路线

app.get('/blogs/:id/comments/new',function(req,res){
    //find blog by id
    Blog.findById(req.params.id,function(err,blog){
        if(err){
            console.log(err)
        }else{
            res.render('comments/new.ejs',{blog:blog})
        }
    })
})
app.post('/blogs/:id/comments',function(req,res){
    //lookup blog using id
    Blog.findById(req.params.id,function(err,blog){
        if(err){
            console.log(err)
        }else{
            Comment.create(req.body.comment,function(err,comment){
                if(err){
                    console.log(err)
                }else{
                    blog.comments.push(comment);
                    blog.save()
                    res.redirect('/blogs/'+blog._id);
                }
            })
        }
    })
})
最后/blogs/:id

app.get('/blogs/:id',function(req,res){
    Blog.findById(req.params.id).populate('comments').exec(function(err,foundBlog){ 
        if(err){
            console.log(err)
            res.redirect('/blogs')
        }else{
            res.render('blogs/show.ejs',{blog:foundBlog})
        }
    })
})
错误

我知道,如果不使用它,很难理解所有这些东西,这就是为什么我会给出我的虚拟文件,让你在其中找到我的项目并进行操作。任何形式的帮助都将不胜感激<谢谢你抽出时间。
提前感谢。

请求正文注释是
{title:'emon',body:'newcomment'}
。这与
commentSchema
中的定义不符。将其更改为符合模式的结构将解决问题。

有什么错误?@CuongLeNgoc先生我更新了我的问题。看,当我从/blogs/:id/comments/new添加一条新评论时,它没有填充blog,而是显示空文本和作者。如果您需要更多信息,请发表评论。再次感谢您更改
blog.comments.push(comment)
blog.comments.push(comment.\u id)会有帮助。让我查一下@cuonglongocSir@CuongLeNgoc先生,同样的问题。显示空文本和作者:)