所以我的Javascript代码存在一些问题。我遇到了这个错误,文档底部的“意外令牌”。我检查了其他堆栈溢出问题,他们都讨论丢失的代码片段。我检查了我的代码,我确信它没有遗漏任何东西。
这是我的代码。 (它使用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 <----
我在这里包含了所有代码,以便更容易找到错误。谢谢, - 坦克
答案 0 :(得分:0)
将 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
});