如何修复Node中的“解析错误:意外令牌”?

时间:2019-04-25 04:37:50

标签: node.js mongodb express

在保存代码(Node JS)时,出现错误“解析错误:意外的令牌” 注意-Mongo已连接

尝试调整花括号和分号,但仍然无法正常工作 我究竟做错了什么? 下面是代码,

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");

//connecting and creating a database
mongoose.connect("mongodb://localhost/yelp_camp");

app.use(bodyParser.urlencoded({extended: true}));

app.set("view engine", "ejs");

//schema setup
var campgroundSchema = new mongoose.Schema({
    name: String,
    url: String
});

var Campground = mongoose.model("Campground", campgroundSchema);
Campground.create( {name: "CampAliBaba", image:"https://photosforclass.com/download/flickr-7121865553"},
                    function(err, campground){
                        if (err){
                            console.log(err);
                    }
                    else {
                        console.log("newly created campground");
                        console.log(campground);
                    }
                    });

 var campgrounds = [
        {name: "Jenny Lake", image:"https://farm2.staticflickr.com/1424/1430198323_c26451b047.jpg"},
        {name: "RichardBH", image:"https://photosforclass.com/download/flickr-7626464792"},
        {name: "CampAliBaba", image:"https://photosforclass.com/download/flickr-7121865553"},
        {name: "CampAliBabaHai", image:"https://photosforclass.com/download/flickr-2770447094"},
        {name: "CampAliBabaHaiYe", image:"https://photosforclass.com/download/flickr-2602356334"},

        ];

app.get("/", function(req, res){
    res.render("landing");
});

app.get("/campgrounds", function(req, res){

        Campground.find({}, function(err, allCampgrouns){
            if(err){
                console.log(err)
            }
            else {
                 res.render("campgrounds", {campgrounds:allCampgrounds});
            }


});

app.post("/campgrounds", function(req, res){

    var name = req.body.name
    var image = req.body.image
    var newcampground = {name: name, image: image}
    campgrounds.push(newcampground);
    res.redirect("/campgrounds");
});

app.get("/campgrounds/new" , function(req, res){
    res.render("new.ejs");
});

app.listen(process.env.PORT, process.env.IP, function(){
    console.log("YelpCamp server started!");
});

预期- 该文件应保存无误,才能启动服务器并运行应用程序

实际- 遇到上述错误

2 个答案:

答案 0 :(得分:1)

app.get("/campgrounds", function(req, res){

    Campground.find({}, function(err, allCampgrouns){
        if(err){
            console.log(err)
        }
        else {
             res.render("campgrounds", {campgrounds:allCampgrounds});
        }
    });// missing the closing brackets
});

您错过了结束标签

答案 1 :(得分:0)

第55行,您应该有一个额外的});

app.get("/campgrounds", function(req, res){

        Campground.find({}, function(err, allCampgrouns){
            if(err){
                console.log(err)
            }
            else {
                 res.render("campgrounds", {campgrounds:allCampgrounds});
            }


});
});
相关问题