节点& Mongo db.open给出undefined不是一个函数

时间:2016-07-09 15:05:49

标签: javascript node.js mongodb api

关注this tutorial,运行我的server.js文件时出现以下错误

self.s.pool = new Pool(Object.^assign(self.s.options, options, {bson:this.s.                       

TypeError: undefined is not a function
"

我的db.open()函数似乎有问题。但是,我不确定是否因为无法与数据库或其他东西建立连接。

我可以记录服务器和数据库对象,因此不确定未定义错误的来源。它没有记录db.open函数中包含的错误消息,所以在此之前似乎有些东西会中断。

任何帮助都会很棒,下面是我的代码

server.js

var express = require('express'),
    movies = require('./routes/movies.js');

var app = express();

app.configure(function(){
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
});

app.get('/movies',movies.findAll);
app.get('/movies/:id',movies.findById);
app.post('/movies', movies.addMovie);
app.put('/movies/:id',movies.updateMovie);
app.delete('/movies/:id',movies.deleteMovie);

app.listen(3000);
console.log('Listening on port 3000...');

路由/ movies.js

var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('moviedb', server);


db.open(function(err,db){
  if(!err)
  {
    console.log('Connected to "moviesdb" database');
    db.collection('movies',{strict:true},function(err,collection){
      if(err)
      {
        console.log("The 'movies' collection doesn't exist. Creating it with sample data...");
        populateDB();
      }
    });
  }
});

exports.findAll = function(req,res){
  db.collection('movies',function(err,collection){
    collection.find().toArray(function(err,items){
      res.send(items);
    });
  });
}

exports.findById = function(req,res){
  var id = req.params.id;
  console.log('Retrieving movie: ' + id);
  db.collection('movies',function(err,collection){
    collection.findOne({'_id':new BSON.ObjectID(id)}, function(err,item){
      res.send(item);
    });
  });
}

exports.addMovie = function(req,res){
  var movie = req.body;
  console.log('Adding movie: ' + JSON.stringify(movie));
  db.collection('movies',function(err,collection){
    collection.insert(movie,{'safe':true},function(err,result){
      if(err)
      {
        res.send({'error':'An error has occurred'});
      }
      else
      {
        console.log('Success:' + JSON.stringify(result[0]));
        res.send(result[0]);
      }
    });
  });
}

exports.updateMovie = function(req,res){
    var id = req.params.id,
        movie = req.body;
    console.log('Updating movie: ' + id);
    console.log(JSON.stringify(movie));
    db.collection('movies',function(err,collection){
      collection.update({'_id':new BSON.ObjectID(id)},movie,{'safe':true},function(err,result){
        if(err)
        {
          console.log('Error updating movie: ' + err);
          res.send({'error':'An error has occured'});
        }
        else
        {
          console.log('' + result + ' document(s) updated');
          res.send(movie);
        }
      });
    });
}

exports.deleteMovie = function(req,res){
  var id = req.params.id;
  console.log('Deleting movie: ' + id);
  db.collection('movies',function(err,collection){
      collection.remove({'_id':new BSON.ObjectID(id)}, {safe:true}, function(err, result) {
        if(err)
        {
          res.send({'error':'An error has occurred - ' + err});
        }
        else
        {
          console.log('' + result + ' document(s) deleted');
          res.send(req.body); 
        }
      });
  });
}

/*--------------------------------------------------------------------------------------------------------------------*/
// Populate database with sample data -- Only used once: the first time the application is started.
var populateDB = function() {

    var movies = [
    {
        name: "Titanic",
        release: "2009",
        description: "description....",
        picture: "titanic-poster.jpg"
    },
     {
        name: "Jaws",
        release: "2000",
        description: "description....",
        picture: "jaws-poster.jpg"
    },
      {
        name: "Halloween",
        release: "1992",
        description: "description....",
        picture: "halloween-poster.jpg"
    }
    ];

    db.collection('movies', function(err, collection) {
        collection.insert(movies, {safe:true}, function(err, result) {});
    });

}

0 个答案:

没有答案