Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
      
like image 614
Kingsfull123 Avatar asked Jan 28 '21 05:01

Kingsfull123


People also ask

What is the relationship between comments and blog post in mongoose?

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.

How do I get Started with a Node JS app?

Let’s get started. Make sure that you have Node.js and NPM installed on your machine, if not, visit the Node.js website to install the latest version. Let’s start by creating a folder for our app.

How do I display a blog post in MongoDB?

Displaying Blog Posts From The Database Now we can remove the “static” pages that represented blog posts from before and use dynamic data from MongoDB to display blog posts. We can modify the /route to now fetch data from MongoDB. Then, we will pass that data to the index.edge file.

What is dynamic blog NodeJS?

Welcome to Dynamic Blog Nodejs tutorial, in which we develop a dynamic website with CRUD operations that might be used to manage the blog articles and pages. SampleBlog is the name of the dynamic ExpressJs application that we’ll create a series of tutorials.


1 Answers

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 realised 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

like image 62
Kingsfull123 Avatar answered Sep 30 '22 15:09

Kingsfull123