Search code examples
javascriptexpresstoken

Issue with Javascript Error: Unexpected Token EXPRESS.JS


So I am having some issues with my Javascript code. I am coming across this error, "Unexpected Token" at the bottom of the document. I checked other stack overflow questions, and they all disscuss missing code pieces. I checked my code, and I am sure that it is not missing anything.

Here is my code. (It uses Express.js and Mongoose)

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










// DATABASE CODE -- IMPORTANT TO ENSURE THAT CODE WORKS. D O  N O T  T O U C H ! ! !

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

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






// main root route
// Partially follows RESTFUL Convention. This site has so many pages it's kinda hard to make it  all crud....  

app.get("/", function(req, res){
   res.render("home"); 
});
//login page
app.get("/login", function(req, res){
   res.render("login"); 
});
//signup
app.get("/signup", function(req, res){
    res.render("createAccount");
});
//user profile page
app.get("/user/:name/:id", function(req, res){
    let name = req.params.name;
    let id = req.params.id;

    res.render("profile", {name: name, id: id});
})

//get req for the forums , name: name, text: text

// var name = posts[{name}];
//     var text = posts[{text}];

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};
        posts.push(newPost)

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

        res.redirect("/forum")
})

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

app.get("/forum/applications", function( req, res){
    res.render("applications");
});
    //THE ERROR IS HERE <---- 

I included all my code here to make it easier to find what is wrong. Thank you, - Tank


Solution

  • Replace your app.get("/forum".. function with this:

     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});
               }  
       }); //this was missing
    });