Creating a Post - Receiving Data

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 345:- Creating a Post - Receiving Data

To receive data for creating a post, we need to create a form in our view that allows users to enter details about their post such as the title, content, and any other relevant information. This form will be submitted to the server using the HTTP POST method.

Here are the steps to receive data for creating a post:

  1. In the posts router, create a new route for handling the HTTP POST request for creating a post.

javascriptCopy code

// Create a new post router.post('/create', async (req, res) => {  // code to create a new post });

  1. In the create route handler, extract the necessary data from the req.body object.

javascriptCopy code

router.post('/create', async (req, res) => {  const { title, content, category } = req.body;  // code to create a new post });

  1. Create a new Post object using the data extracted from the req.body object.

javascriptCopy code

router.post('/create', async (req, res) => {  const { title, content, category } = req.body;  const post = new Post({    title,    content,    category,    author: req.user._id,  });  // code to save the new post to the database });

  1. Call the save method on the Post object to save the new post to the database.

javascriptCopy code

router.post('/create', async (req, res) => {  const { title, content, category } = req.body;  const post = new Post({    title,    content,    category,    author: req.user._id,  });  await post.save();  // code to redirect the user to the newly created post });

  1. Once the post is saved to the database, redirect the user to the newly created post using the res.redirect method.

javascriptCopy code

router.post('/create', async (req, res) => {  const { title, content, category } = req.body;  const post = new Post({    title,    content,    category,    author: req.user._id,  });  await post.save();  res.redirect(`/posts/${post._id}`); });

  1. In the view for creating a post, create a form that uses the HTTP POST method to submit the data to the server.

htmlCopy code

<form action="/posts/create" method="post">  <div>    <label for="title">Title</label>    <input type="text" name="title" id="title">  </div>  <div>    <label for="content">Content</label>    <textarea name="content" id="content"></textarea>  </div>  <div>    <label for="category">Category</label>    <input type="text" name="category" id="category">  </div>  <button type="submit">Create Post</button> </form>

  1. When the user submits the form, the data will be sent to the server using the HTTP POST method. The router.post('/create') route handler will handle the request, extract the data from the req.body object, create a new Post object using the data, and save it to the database. Finally, the user will be redirected to the newly created post.
40. Converting To Ajax

2 Comments

@niteshguptav63
niteshguptav63 Nov 17, 2024 at 1:39 PM

I am not able to access videos from second class and further. I have already completed first class

@niteshguptav63
niteshguptav63 Nov 16, 2024 at 10:56 AM

When will I get my course?

@admin79
admin79 Nov 17, 2024 at 1:29 PM

Now, Your query was resolved.

Frequently Asked Questions About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support