Chaitanaya Sethi
Chaitanaya Sethi

Reputation: 39

How to fix 'Parsing error: Unexpected token' in Node?

On saving the code (Node JS), getting the error, 'Parsing error: Unexpected Token' Note - Mongo connected

Tried adjusting the curly brackets and semicolon, still not working What am I doing wrong? Below is the code,

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

Expected- The file should save error-free in order to start the server and run the application

Actual- Getting above mentioned error

Upvotes: 0

Views: 1477

Answers (2)

Narendra Chouhan
Narendra Chouhan

Reputation: 2319

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

you have miss the closing tag

Upvotes: 2

Huy Ngo
Huy Ngo

Reputation: 382

On line 55, you should have an extra });

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

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


});
});

Upvotes: 0

Related Questions