0

PleaseI am working on a blog site with nodejs/express, mongoDB and using Pug . I am currently trying to send comments to my post data using a patch request from Pug and having some issues. Below is the details.

I have a server folder in root directory which has a blogPost.js file. This blogPost.js file contains the code below

...
router.patch('/comment/:postId', async (req, res) => {
    try{
    const updatedPostComment = await BlogPost.updateOne({ _id: req.params.postId }, 
        {
            $push: {
                comment: [{
                    author: req.body.comment.author,
                    text: req.body.comment.text   
            }]}      
            });
        res.json(updatedPostComment);
            }catch (err) {
                res.json({ message: err });
    }
});
...

This code works fine when I test it with postman. I am able to make a patch request from postman to update the comment array in my data for a specific post in mongoDb based on the Id of the post.

I also have an index.js file in the root folder which has the code below,

...
//Set Pug
app.set('view engine', 'pug');

//Pug Routes/Request
app.get('/', async (req, res) => {
    const query = await axios.get('http://localhost:1800/post');
    res.render('index', { posts: query.data });
  });
...
app.patch('/comment/:id', async (req, res) => {
    const query = await axios.patch('http://localhost:1800/post/comment/' + req.params.id);
    res.render('comment', { 
      id: req.params.id
     });
  });

// Connect to DB
mongoose.connect( 
    process.env.DB_CONNECTION, 
    {  useNewUrlParser: true ,
       useUnifiedTopology: true, 
    },
    () => console.log('Connected to DB')
);

const port = process.env.PORT || 1800;
app.listen(port, () => console.log(`server started on port ${port}`));

and finally, I have index.pug file in my views folder which has a form in the code below

extends layout
block content
    br
    - const post = posts[0]

        div
            form(action=`http://localhost:1800/post/comment/${post._id}` method="patch" class="was-validated shadow font-weight-bold")
                .form-group.mx-3
                    label(for="" class="text-uppercase") Author
                    input(type="text" name="author" id="" class="form-control" required)
                .form-group.mx-3
                    label(for="" class="text-uppercase") Comment
                    input(type="text" name="text" id="" class="form-control" required)
                .text-center
                    input(type="submit" value="Send Post" class="btn btn-primary mt-4 mb-5")

When I try to send the patch request from the form, it does not work as it worked from the postman. This is the error i have in my console and url when send text to the form

get http://localhost:1800/post/comment/5ebcf79271217d299c6d2f1eauthor=Kwabena+Attakora+Frimpong&text=thank+you+God 404 error

Please what am I missing. Is it from the form action or am missing something in the indexjs file. Pleae I am stack and I need help. thank you for your support.

1 Answers1

-1

Here 2023 I have the same problem. Nevertherless I have a "solution" But you are not going to like it neither I do not know why's that happening but you need to change PATCH to POST method in your PUG engine also on your server use the post method.

Just switch those words and It will work, 100% guaranteed!