程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了无法 POST nodejs 和 mongodb大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决无法 POST nodejs 和 mongodb?

开发过程中遇到无法 POST nodejs 和 mongodb的问题如何解决?下面主要结合日常开发的经验,给出你关于无法 POST nodejs 和 mongodb的解决方法建议,希望对你解决无法 POST nodejs 和 mongodb有所启发或帮助; @H_772_2@我正在尝试向博客页面上的博客添加评论,但我找不到错误,单击表单上的提交按钮后,我一直收到错误“无法发布 /”。缺少什么,有什么问题?我为我的表单尝试了以下代码:

@H_772_2@这是我该页面的完整 .eJs 代码。 '''

  <!DOCTYPE HTML>
<HTML>
<head>
<Meta name="vIEwport" content="wIDth=device-wIDth,initial-scale=1">
<link rel="stylesheet" href="/static/user/CSS/style.CSS">
</head>
<body>
<div class="row">
  <div class="leftcolumn">
    <div class="card">
      <h2><%=post.title%></h2>
      <h5>title description,Dec 7,2017</h5>
      <div class="fakeimg" style="height:200px;">Image</div>
      <p><%=post.content%></p>
    </div>
    <div ID="respond" class="comment-respond">
      <h3 ID="reply-title" class="comment-reply-title">
        Leave a Reply <small><a rel="nofollow" ID="cancel-comment-reply-link" href="/warning-over-dangerous-diy-beauty-trends-on-tiktok/#respond" style="display:none;">Cancel reply</a></small>
      </h3>
      <form method="POST" onsubmit="return doComment(this);">
        <input type=""  name="post_ID" value="<%=post._ID%>" />
        
        <div class='form-group'>  
          <textarea placeholder="Comment" name="comment" cols="45" rows="8" required></textarea>
        </div>

        <div class='form-group'>  
          <input placeholder="name (required)" name="author" type="text"  required />
        </div>
        
        <div class='form-group'>  
          <input  placeholder="Email (required)" name="email" type="email" required />
        </div>
          <input type="submit" class="waves-effect waves-light btn-large" value="Post Comment">
      </form> 

    <script>
       function doComment(form){
          $.AJAX({
              url:"/do-comment",method:"POST",data:{author: form.author.value,comment: form.comment.value,post_ID: form.post_ID.value,email: form.email.value},success: function (responsE){
                  alert(responsE);
              }
          });
          return false;
        }
    </script>
    </div>
  </div>
  <div class="rightcolumn">
    <div class="card">
      <h2>About Me</h2>
      <div class="fakeimg" style="height:100px;">Image</div>
      <p>Some text about me in culpa qui officia deserunt mollit anim..</p>
    </div>
    <div class="card">
      <h3>Popular Post</h3>
   
      <div class="fakeimg"><a href="<%=post._ID%>">Image</a></div><br>
  
    </div>
    <div class="card">
      <h3>Follow Me</h3>
      <p>Some text..</p>
    </div>
  </div>
</div>

<%- include('footer'); -%>

</body>
</HTML>
@H_772_2@'''

@H_772_2@这是我编写的完整 .Js 代码:

@H_772_2@'''

 var express =require("express");
var app=express();
var bodyParser=require("body-parser");
var ObjectID = require("mongodb").objectID;
app.use("/static",express.static(__dirname + "/static"));
app.set("vIEw ENGIne","eJs");
    app.use(bodyParser.urlencoded({
        extended : false
      }));
  app.use(bodyParser.Json());
var MongoClIEnt =require("mongodb").MongoClIEnt;
MongoClIEnt.connect("mongodb://localhost:27017",{useUnifIEdtopology: truE},function(error,clIEnt){
        var blog=clIEnt.db("blog");
        console.log("DB Connected");
        app.get('/',function(req,res) {
            blog.collection("posts").find().sort({"_ID":-1}).toArray(function(error,posts){

            res.render("user/home",{posts: posts}) ;
            });
        });
        app.get("/admin/dashboard",res) {
            res.render("admin/dashboard");
        });
        app.get("/admin/posts",res) {
            res.render("admin/posts");
        });
        app.post("/do-post",res){
            blog.collection("posts").insertOne(req.body,document){
                res.send("posted succesfully");
            });
        });
        app.get("/posts/:ID",res){
            console.log("Comment");
            blog.collection("posts").findOne({"_ID": ObjectID(req.params.ID)},function (error,post) {
                res.render("user/post",{post: post});
            });
        });
        app.post("/do-comment",res){
            blog.collection("posts").update({"_ID": ObjectID(req.body.ID) },{ 
                $push: { 
                    "comments": {author: req.body.author,comment: req.body.comment,email: req.body.email } 
                }
            },post) {
                res.send("comment Sucessfull");
            });
        });
        app.Listen(3000,function(){
            console.log("Connected");
        });

    });
@H_772_2@'''

解决方法

@H_772_2@暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

@H_772_2@如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

@H_772_2@小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的无法 POST nodejs 和 mongodb全部内容,希望文章能够帮你解决无法 POST nodejs 和 mongodb所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。