调用Node.js中的函数

时间:2015-02-18 16:03:38

标签: javascript node.js

var http = require("http");
var url = require("url");
var path = require("path");
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('gamedb', server);
db.open(function(err, db) {
   if (!err) {
       console.log("Connected to gameapp database");
       db.collection('games', {
           strict: true
       }, function(err, collection) {
           if (err) {
               console.log("cant connect to db");
           }
       });
   }
});

var findAll = function(req, res) {
   db.collection('games', function(err, collection) {
       collection.find().limit(10).toArray(function(err, items) {
           res.send(items);
       });
   });
};

http.createServer(function(req, res) {
   if (req.url == "/games" & req.method == "GET") {
       res.writeHead(200, {
           'Content-Type': 'text/plain'
       });
       findAll(req, res);
       res.end('end request\n');
   } else {
       res.writeHead(200, {
           'Content-Type': 'text/plain'
       });
       res.end('HI\n');
   }

}).listen(3000, '127.0.0.1');
console.log('Server running at http://127.0.0.1:3000/');

我一直试图在我的创建服务器函数中调用findAll(),但我总是收到一条错误消息undefined is not a function。我仔细检查了我的语法,我找不到任何错误。有什么见解吗?

编辑:

确切的错误消息:

Connected to gameapp database
Server running at http://127.0.0.1:3000/
/Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/connection/base.js:246
        throw message;      
              ^
TypeError: undefined is not a function
    at /Users/Justin/Documents/git/CISC474/gameapp/server1.js:29:18
    at /Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:197:9
    at /Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:228:31
    at /Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:806:30
    at Cursor.close (/Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:1009:5)
    at getMore (/Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:806:12)
    at getAllByGetMore (/Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:226:3)
    at /Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:184:7
    at commandHandler (/Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/cursor.js:734:16)
    at /Users/Justin/Documents/git/CISC474/gameapp/node_modules/mongodb/lib/mongodb/db.js:1903:9

编辑:

解决方案:

var findAll = function(req, res) {
     db.collection('games', function(err, collection) {
         collection.find().limit(10).toArray(function(err, items) {
        var result = JSON.stringify(items);
             res.write(result);
              res.end('end request\n');

1 个答案:

答案 0 :(得分:1)

问题在于你调用db.collection(' games'),因为此范围内的db引用

db = new Db('gamedb', server)  

而不是

回调定义的实际数据库连接
db.open(function(err, db) 

编辑:仅在您建立数据库连接后启动服务器

var http = require("http");
var url = require("url");
var path = require("path");
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('gamedb', server);
db.open(function(err, db) {
   if (!err) {
       console.log("Connected to gameapp database");
       db.collection('games', {
           strict: true
       }, function(err, collection) {
           if (err) {
               console.log("cant connect to db");
           }
       });
   }

   var findAll = function(req, res) {
     db.collection('games', function(err, collection) {
         collection.find().limit(10).toArray(function(err, items) {
             res.send(items);
         });
     });
  };

  http.createServer(function(req, res) {
     if (req.url == "/games" & req.method == "GET") {
         res.writeHead(200, {
             'Content-Type': 'text/plain'
         });
         findAll(req, res);

         // comment this out
         // res.end('end request\n');
     } else {
         res.writeHead(200, {
             'Content-Type': 'text/plain'
         });
         res.end('HI\n');
     }

  }).listen(3000, '127.0.0.1');

  console.log('Server running at http://127.0.0.1:3000/');
});