'using postman data is sent to POST method and saved but in some strange string
I am begineer in node js and have to make college project in very little time due to this sem being a crunch sem. so I tried to make a comment system for my blog using node js college project. So far, articles can be created,updated,deleted and read. POST comment works when using Postman and also redirects to proper page. The post method saves comment and it is also visible on concerned page, However the string that I type in Postman as comment gets changes to some random String which is shown in the included picture. I need a solution to fix this problem and as a begineer any help, pointers are apreciated.
My schema for comments and article
//comment.Schema
const mongoose= require('mongoose')
var commentSchema = new mongoose.Schema(
{
text:{
type: String,
required:true
},
date:{
type:Date,
default: Date.now
},
article:{
type: mongoose.Schema.Types.ObjectId,
ref: 'Article'
}
}
)
module.exports=mongoose.model('Comment',commentSchema)
//Article schema
const mongoose = require('mongoose')
//Define Database schema
const articleSchema=new mongoose.Schema({
title:{
type:String,
required: true
},
description:{
type:String,
required:true
},
markdown:{
type:String,
required:true
},
createdAt:{
type: Date,
default: Date.now
},
slug:{
type:String,
required:true,
unique:true
},
comments:[{
type: String,
type:mongoose.Schema.Types.ObjectId,
ref:'Comment'
}],
})
//export Article model
module.exports=mongoose.model('Article',articleSchema)
//POST request to save a comment
router.post('/edit/:id/comments', async (req,res)=>{
const id = req.params.id;
const comment = new Comment({
text: req.body.comment,
Article: id
})
await comment.save();
const ArticleRelated= await Article.findById(id);
ArticleRelated.comments.push(comment);
await ArticleRelated.save(function(err) {
if(err) {console.log(err)}
res.redirect('/')
})
//ejs file to show Article and comments
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<title>Blog project</title>
</head>
<body>
<div class="container">
<h1 class="mb-4"><%= article.title %></h1>
<div class="text-muted mb-2">
<%= article.createdAt.toLocaleDateString() %>
</div>
<a href="/" class="btn btn-secondary">All Articles</a>
<a href="/articles/edit/<%= article.id %>" class="btn btn-info">edit</a>
<div>
<%- article.sanitizedHtml %>
</div>
<h2>Comments</h2>
<div>
<a href="/articles/edit/<%= article.id %>/comments" class="btn btn-info">Add Comment</a>
<div>
<%= article.comments %>
</div>
</div>
</div>
</body>
</html>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
