Post request problems: Post request works, but text from the form does not show up

26 views Asked by At

so I am having some problems with my post request code. I have a form, which is for a basic forum system. It does post and adds the data to the database, however, the actual data, the post entered into the form does not work. Here is my code. (It uses express and mongoose, and it is pretty big)

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

Here is the actual forum page itself.

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

Here is the form where a post is created. Again, the data that is inputted in the form does not show up.

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

Thank you, Sam

0

There are 0 answers