发布请求问题:发布请求有效,但表单中的文本未显示

时间:2018-04-03 13:50:01

标签: express post mongoose python-requests forums

所以我的帖子请求代码有问题。我有一个表单,用于基本的论坛系统。它确实发布并将数据添加到数据库中,但是,实际数据,输入到表单中的帖子不起作用。 这是我的代码。 (它使用快递和猫鼬,而且非常大)

   const express = require("express");
const app = express();
const mongoose = require("mongoose")
mongoose.connect("mongodb://localhost/cba_database")
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({extended: true}));
app.set("view engine", "ejs");
app.use(express.static("public")); 
app.listen(process.env.PORT, process.env.IP, function(){
    console.log("==========================")
    console.log("THE CBA SERVER HAS STARTED")
    console.log("==========================")
});




//defining the schema for a new forum post
let postSchema = new mongoose.Schema({
    name: String,
    text: String

});
const Post = mongoose.model("Post", postSchema)









 app.get("/forum", function(req, res){       
//checking for errors, console logging them, otherwise rendering forum template. 
  Post.find({}, function(err, allPosts){
          if(err) {
            console.log("Oh no.. error!!");
            console.log(err);
          } else {
             res.render("forum", {posts: allPosts});
           }  
   }); 
});



//forums and minigames 
app.post("/forum", function(req, res){
    //temporary code. Will be changed when database is set up

        let text = req.body.text;
        let name = req.body.name;
        let newPost = {text: text, name: name};


         Post.create(newPost, function(err, newlyMade){
            if(err) {
                console.log("Oh no.. error!!");
                console.log(err);
           } else {
                 res.redirect("/forum");
            }  
        })


})

app.get("/forum/createPost", function(req, res){
   res.render("newPost.ejs") 
});

这是实际的论坛页面。

<% include partials/header %>

<style>
    .top {
        margin-top: -20px;
          width: 100%;
          height: 100%;
          display: block;

          padding: 40px;
          background: #2d3436;
          color: white;
          font-family: "Open sans", sans-serif, helvetica;

    }
    .title {
        font-weight: 400;
        font-family: "Open sans",sans-serif,helvetica;
    }
    body {
        background: #dfe6e9;
    }
</style>





<div class="top">
    <h1 class="title">Creative Blocks Forum</h1>

</div>

<button><a href="/forum/createPost">Add post</a></button>

<% posts.forEach(function(post){ %>
<h1>Post Written By: <%= posts.name %> </h1>
<p>Post Content:<%= posts.text %></p>


<% }) %>








<% include partials/footer %>

以下是创建帖子的表单。同样,表单中输入的数据也没有显示出来。

<% include partials/header %>

<div class="container">
     <div style="margin: 25px auto; width: 30%;">-->   <!-- COMMENTED OUT CODE... SOON TO BE REMOVED. USED AS A REF -->
              <form action="/forum" method="POST">
                  <div class="form-group">
                       <input class="input inlineText" type="text" name="text" placeholder="Enter text here.">
                  </div>
                  <div class="form-group">
                      <input class="input"  type="text" name="name"  placeholder="Your name">
                  </div>

                <div class="form-group">
                       <button class="btn btn-lg btn-success btn-block" href="/forum" type="submit">Submit</button>
                  </div>
               <a>Return to forum.</a>
          </div>    

    </form>
     </div>






<% include partials/footer %>

谢谢你, 萨姆

0 个答案:

没有答案