使用 NodeJs 更新 MongoDB 操作

发布于 2025-01-13 10:54:45 字数 1927 浏览 0 评论 0原文

我正在开发一个具有 CRUD 操作的博客网站,我能够实现 CRD 操作,但更新一有问题。

问题:-

当我单击“编辑”按钮时,它会打开“撰写”选项卡,其中文本文件已成功加载,但是当我单击“更新”时,它会重定向到主页,但没有任何更新,请帮助我摆脱这种情况。

//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("/");
          }
        }
      );
    });

编辑/更新表格

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

城歌 2025-01-20 10:54:45

你的错误来自Ejs

你没有很好地重命名你的req.body来匹配你传入的数据

你应该使用标题而不是postTitle

和内容而不是博客

只需编辑你的ejs到此并测试哎呀

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

如果这回答了你的问题将其标记为已完成

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文