Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

node.js - Node JS mongoose create a blog post commenting system

I am trying to build a simple blog with a commenting system with mongoose and express. There is no issse of creating and posting blogs here and each post can be displayed correctly. However, there are some issues associated with comments and each blog. The relationship between comments and blog post was established by applying mongoose.Schema.Types.ObjectId in the post Schema and the comments has been created to store array of comments ids. I think the schema structures are correct and there might be some problems in my code for routing and please help, thanks.

    // Post Schema
    const mongoose = require('mongoose');

    const postSchema = new mongoose.Schema({
      title: {
       type: String,
       trim: true,
       required: true
     },
       text: {
       type: String,
       trim: true,
       required: true
     },
      date: {
       type: Date,
       default: Date.now
     },
      comments: [{
       type: mongoose.Schema.Types.ObjectId,
       ref: 'Comment'
      }]
    })

    postSchema.virtual('url').get(function(){
      return '/post/' + this._id
     })

    module.exports = mongoose.model('Post', postSchema); 


     // Comment Schema 
     
     const mongoose = require('mongoose');

     const commentSchema = new mongoose.Schema({
        text: {
         type: String,
         trim: true,
         required: true
        },
        date: {
         type: Date,
         default: Date.now
         }
        })

      module.exports = mongoose.model('Comment', commentSchema); 

      // Router 

       const express = require('express');
       const Post = require('../models/post');
       const Comment = require('../models/comment');

       const router = new express.Router();


       // Get comments
       router.get('/post/:id/comment', (req, res) => {
           res.render('post-comment', {title: 'Post a comment'})
       })


       // Create and Post comments, this is where I think I made mistakes

       router.post('/post/:id/comment', async (req, res) => {
           const comment = new Comment({text: req.body.text});
           const post = await Post.findById(req.params.id);
           const savedPost = post.comments.push(comment);

           savedPost.save(function(err, results){
              if(err) {console.log(err)}
              res.render('post_details', {title: 'Post details', comments: 
               results.comments})
            } )
          })


        //  Get each post details. 
        // Trying to display comments, but it is all empty and I realized 
        // the comments array is empty, I can see the comments create in 
        // mongodb database why is that?????

       router.get('/post/:id', (req, res) => {
         Post.findById(req.params.id)
             .populate('comments')
             .exec(function(err, results) {
           if(err) {console.log(err)}
             res.render('post_details', {title: 'Post details', post: 
          results, comments: results.comments})
           })
         })

       router.get('/new', (req, res) => {
         res.render('create-post', {title: 'Create a post'})
        })

       router.post('/new', (req, res) => {
         const post = new Post({
          title: req.body.title,
          text: req.body.text
          });
          post.save(function(err) {
           if(err) {console.log(err)}
             res.redirect('/')
          })
         })

       router.get('/', (req, res) => {
          Post.find()
             .exec(function(err, results) {
              if(err) {console.log(err)}

              res.render('posts', {title: 'All Posts', posts: results})
           })
       });

      module.exports = router;
      
question from:https://stackoverflow.com/questions/65931572/node-js-mongoose-create-a-blog-post-commenting-system

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It has been a few days after I post this question and I have received no answer so far. But I have figure out why my code is not working and this post will try to answer this question I asked a few days ago and hope it can help some who is struggling the same issue. My problems here are each comment created can not been pushed into the array of comments in Post, so the comments can not be displayed as the array is empty. If you look at my code for Schema, you might realized that I made mistake for comment schema as I did not define a post key value pair, so the correct comment and post schema should be as below. The logic here is that each blog post can has multiple comments below, so the comments in post Scheme should be created as a array, but each comment can only below to one of the post, therefore the post key value pair in comment schema should be only in a object

        const mongoose = require('mongoose');

        const commentSchema = new mongoose.Schema({
         text: {
              type: String,
              trim: true,
              required: true
           },
        date: {
              type: Date,
              default: Date.now
           },
       // each comment can only relates to one blog, so it's not in array
        post: {
              type: mongoose.Schema.Types.ObjectId,
              ref: 'Post'
           }
         })

        module.exports = mongoose.model('Comment', commentSchema);

and the post schema should be as below

        const mongoose = require('mongoose');

        const postSchema = new mongoose.Schema({
           title: {
             type: String,
             trim: true,
              required: true
           },
           text: {
             type: String,
             trim: true,
             required: true
           },
           date: {
             type: Date,
             default: Date.now
            },
         // a blog post can have multiple comments, so it should be in a array.
         // all comments info should be kept in this array of this blog post.
          comments: [{
             type: mongoose.Schema.Types.ObjectId,
             ref: 'Comment'
           }]
           })

           postSchema.virtual('url').get(function(){
              return '/post/' + this._id
           })

         module.exports = mongoose.model('Post', postSchema);

Another important thing I did not do was, every time a comment was posted, this comment should be pushed into the post.comments array. I did not do this step, that's why my array was always empty and there is no comment to display. the code to correct this is go to file commentRouter.js (I have created both of postRouter.js and commentRouter.js), add code below

         router.post('/post/:id/comment', async (req, res) => {
              // find out which post you are commenting
               const id = req.params.id;
              // get the comment text and record post id
               const comment = new Comment({
               text: req.body.comment,
               post: id
            })
              // save comment
           await comment.save();
              // get this particular post
           const postRelated = await Post.findById(id);
              // push the comment into the post.comments array
           postRelated.comments.push(comment);
              // save and redirect...
           await postRelated.save(function(err) {
           if(err) {console.log(err)}
           res.redirect('/')
           })

          })

the above is how I fixed my error, hope it can help someone.

Thank you for reading


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...