0
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/');

I keep trying to call findAll() in my create server function but I always get an error message saying undefined is not a function. I double checked my syntax and I can't find any errors. Any insight?

EDIT:

EXACT ERROR MESSAGE:

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

EDIT:

SOLUTION:

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');
user2510809
  • 235
  • 3
  • 14
  • I suspect that the error is caused by calling res.send() after res.end() (since res.end() happens a long time before the db query returns any results). If res is no longer a connection (due to res.end()) then res.send may be undefined and trying to call it will result in the error – slebetman Feb 18 '15 at 16:11
  • 1
    1) Stop defining functions like that if you don't know why. 2) Read about variable scope: http://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript – Sergiu Paraschiv Feb 18 '15 at 16:11
  • 1
    @SergiuParaschiv: Scope looks fine to me. I suspect it's an asynchronous thing. – slebetman Feb 18 '15 at 16:13
  • 1
    @slebetman Would agree, `findAll` is async, so OP should use the function in async manner. – chridam Feb 18 '15 at 16:15

1 Answers1

1

The issue is where you call db.collection('games'), because db in this scope refers to

db = new Db('gamedb', server)  

rather than the actual db connection as defined by the callback of

db.open(function(err, db) 

EDIT: Start the server only after you've established a database connection

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/');
});
Yuri Zarubin
  • 11,439
  • 4
  • 30
  • 33
  • i tried this but when I connect to the Db i get the error message that the db is already open in my findAll() function – user2510809 Feb 18 '15 at 16:34
  • I've edited my answer. There's a few things wrong with your code, but the easiest way to fix them is to start the server after you open the database connection. – Yuri Zarubin Feb 18 '15 at 16:56
  • I still get the same error. I have added the exact error message I get in the OP. – user2510809 Feb 18 '15 at 18:02
  • db.collection('games', function(err, collection) is throwing an error, which you aren't handling, and instead calling collection.find(). console.log(err) to see what the error is. – Yuri Zarubin Feb 18 '15 at 18:22
  • I found the error. I was using res.send with is only included in the express framework which i am not using. Instead I use res.write(items). However When I do that, on the webpage it says "waiting for localhost" and a response never comes back. it just keeps waiting forever. – user2510809 Feb 18 '15 at 18:32
  • Did you call res.end() afterwards? – Yuri Zarubin Feb 18 '15 at 18:46
  • Hey I figured it out and res.end() wasnt the problem but it would of been another problem if you had not said anything. So I just wanted to say Yuri Zarubin your the man! thanks so much for helping me out! In the OP I posted the working function. – user2510809 Feb 18 '15 at 19:08