【问题标题】:Issue with Javascript Error: Unexpected Token EXPRESS.JSJavascript 错误问题:意外的令牌 EXPRESS.JS
【发布时间】:2018-04-01 16:10:46
【问题描述】:

所以我的 Javascript 代码有一些问题。我在文档底部遇到了这个错误,“Unexpected Token”。我检查了其他堆栈溢出问题,他们都在讨论丢失的代码片段。我检查了我的代码,我确信它没有遗漏任何东西。

这是我的代码。 (它使用 Express.js 和 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 <---- 

我在这里包含了我的所有代码,以便更容易找到问题所在。谢谢, - 坦克

【问题讨论】:

    标签: javascript express token


    【解决方案1】:

    用这个替换你的 app.get("/forum".. 函数:

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

    【讨论】:

      猜你喜欢
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 1970-01-01
      • 1970-01-01
      • 2017-07-12
      • 2017-09-19
      • 1970-01-01
      相关资源
      最近更新 更多