Node / Express / Mongo DB / Mongoose: - 意外的标识符

时间:2015-08-13 06:11:56

标签: node.js mongodb

我是node.js的新手。我创建了一个名为app.js的文件,并使用express将该代码放入该文件中以切换模板引擎:

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

当我运行应用程序时,我收到此错误:

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

此行导致错误:

app.use('/',router);

当我注释掉这一行时,我也开始出现以下错误:

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

然后

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

最后

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

2 个答案:

答案 0 :(得分:1)

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

这意味着大括号没有关闭。检查这个支架 - > }计数是正确的

答案 1 :(得分:0)

App.get(“/ 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
          });
        });
      }
})});