Francesca Tabor
Francesca Tabor

Reputation: 1

Node / Express / Mongo DB / Mongoose : - Unexpected identifier

I am new to node.js. I created a file named app.js and put this code in that file using express to switch the template engine:

var path = require('path');
var express = require('express');
var app = express();
var expressLayouts = require('express-ejs-layouts'    );
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }))    ;

app.set('views', path.join(__dirname, 'views'));
app.engine('ejs', require('ejs').renderFile);

var debug = require("debug");
var logger = require('morgan');
app.use(logger('dev'));

var mongoose = require('mongoose');
mongoose.createConnection('mongodb://localhost/animalshelter');

var port = process.env.PORT || 9000; 
var router = express.Router()
var animalRouter = express.Router(); 


///////////////// ROOTES //////////////////////

var showanimals = require("../models/animal.js");


// Animal HOME
app.get("/", function (req, res){
  res.redirect('/animals');
});


// Animals INDEX
app.get("/animals", function (req, res){
  res.render(path.join(__dirname + '/public/views/    index.ejs')); 

  Animal.find({}).exec(function(err, animals) {
    if(err) {
      console.log("Error:", err);
    }
    else {
      res.render("index", {animals: animals});
    }
  });
});


// NEW Animal Form

app.get("/animals/new", function (req, res){
 res.render(path.join(__dirname + '/public/views/    new.ejs'));
});


// CREATE Animals

app.post("/animals", function(req, res) {
    console.log("POST DATA", req.body);
    var Animal =  new Animal(req.body); 

    animal.save(function(err) {
      if(err) {
        console.log("Animal not saved in MongoDB     'animals' database.")
      }
      else {
        console.log("Successfully updated an     animal.");
        res.redirect("/");
      }
    });
  });


// SHOW an Animal

app.get("/animals/:id", function(req, res) {
  Animal.findOne(
    {_id:     req.params.id}, 
    {name:      req.body.name}, 
    {breed:    req.body.breed},
    {dob:      req.body.dob},
    {gender:   req.body.gender},
    {family:   req.body.family},
    {status:   req.body.status}, 

    function(err, animal) {
      if(err) {
        console.log("Animal not saved in MongoDB     'animals' database.")
      }
      else {
        console.log("Successfully updated an     animal.");
        res.redirect("/");
      };


// UPDATE an Animal

app.put("/animals/:id", function(req, res) {
    Animal.update(req.body),

      function(err, animal) {
        if(err) {
          console.log("Animal not saved in     MongoDB 'animals' database.")
        }
        else {
          console.log("Successfully updated an     animal.");
          res.redirect("/");
        };


// DELETE Animals 

app.get("/animals/:id/destroy", function(req, res)     {

    Animal.remove({_id: req.params.id}, function(    err, animal) {
      if(err) {
        console.log("Animal not saved in MongoDB     'animals' database.")
      }
      else {
        console.log("Animal successfully deleted!"    );
        res.redirect("/");
        res.redirect("/");
      };


      if (app.get('env') === 'development') {
        app.use(function(err, req, res, next) {
          res.status(err.status || 500);
          res.send('error', {
            message: err.message,
            error: err
          });
        });
      }
}

// Listen

app.use('/', router);
app.use('/animals', animalRouter);

app.listen(port, function(){
console.log('Server started on', port);
});

When I run the application and I get this error :

app.use('/', router);
^^^
SyntaxError: Unexpected identifier
   at exports.runInThisContext (vm.js:73:16)
   at Module._compile (module.js:443:25)
   at Object.Module._extensions..js (module.js:478:10)
   at Module.load (module.js:355:32)
   at Function.Module._load (module.js:310:12)
   at Function.Module.runMain (module.js:501:10)
   at startup (node.js:129:16)
   at node.js:814:3

This line is causing the error:

app.use('/', router);

When I commented out this line I proceeded to get the following errors also:

 app.use('/animals', animalRouter);
^^^
 SyntaxError: Unexpected identifier

then

app.listen(port, function(){
^^^
SyntaxError: Unexpected identifier

and finally

app.js:191
});
^
SyntaxError: Unexpected token }

Upvotes: 0

Views: 1780

Answers (2)

app.js:191
});
^
SyntaxError: Unexpected token }

This means the braces didnt close. Check if this brace --> } count is proper

Upvotes: 1

ecyshor
ecyshor

Reputation: 1439

The App.get("/animals..... function call is not closed correctly. You are missing )}); Try like this:

app.get("/animals/:id/destroy", function(req, res)     {

    Animal.remove({_id: req.params.id}, function(    err, animal) {
      if(err) {
        console.log("Animal not saved in MongoDB     'animals' database.")
      }
      else {
        console.log("Animal successfully deleted!"    );
        res.redirect("/");
        res.redirect("/");
      };


      if (app.get('env') === 'development') {
        app.use(function(err, req, res, next) {
          res.status(err.status || 500);
          res.send('error', {
            message: err.message,
            error: err
          });
        });
      }
})});

Upvotes: 0

Related Questions