0

I am working on a Blog WebSite with CRUD operation I am able to achieve CRD operations but have issues with update One.

Issue:-

When I click on the edit button it opens the compose tab with the textfile loaded successfully but when I click on update it redirects to the home page but nothing Update, Please help me get out from this.

//This is the code for edit & Update Route 
        app.get("/posts/:postId/edit", (req, res) => {
      Post.findById(req.params.postId, (err, post) => {
        if (err) {
          console.log(err);
        } else {
          res.render("edit", { post: post });
        }
      });
    });
    app.post("/posts/:postId/edit", (req, res) => {
      Post.findByIdAndUpdate(
        req.params.postId,
        {
          $set: {
            title: req.body.title,
            content: req.body.content,
          },
        },
        (err, update) => {
          if (err) {
            console.log(err);
          } else {
            console.log("Post Updated");
            res.redirect("/");
          }
        }
      );
    });

Form for the Edit/Update

//This is the Edit ejs file containing the update form 
 <form action="/posts/<%=post._id%>/edit/?_method=PUT" method="post">
            <div class="mb-3">
                <label for="post-title">Title</label>
                <input class="form-control" id="post-title" name="postTitle" type="text" placeholder="Input title"
                    required autocomplete="off" value="<%=post.title%>">
            </div>
            <div class="mb-3">
                <label for="postcontent">Post</label>
                <textarea class="form-control text-area" id="postcontent" name="blog" rows="4" cols="50" required
                    placeholder="Start writing your blog ..............."><%=post.content%></textarea>
            </div>
            <button class="btn btn-primary publish-btn" type="submit" name="button">Update</button>
        </form>
2
  • yes it is correct Commented Mar 11, 2022 at 13:53
  • My issue resolved thanks for your time Commented Mar 11, 2022 at 14:08

1 Answer 1

1

Your error is from the Ejs

You didn’t rename your req.body well to match your incoming data

You were meant to be used title instead of postTitle

And content instead of blog

Just edit your ejs to this and test gee

 <form action="/posts/<%=post._id%>/edit/?_method=PUT" method="post">
        <div class="mb-3">
            <label for="post-title">Title</label>
            <input class="form-control" id="post-title" name=“title" type="text" placeholder="Input title"
                required autocomplete="off" value="<%=post.title%>">
        </div>
        <div class="mb-3">
            <label for="postcontent">Post</label>
            <textarea class="form-control text-area" id="postcontent" name="content" rows="4" cols="50" required
                placeholder="Start writing your blog ..............."><%=post.content%></textarea>
        </div>
        <button class="btn btn-primary publish-btn" type="submit" name="button">Update</button>
    </form>

If this answers your question mark it as done

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.