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');
4
  • 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 Commented Feb 18, 2015 at 16:11
  • 1
    1) Stop defining functions like that if you don't know why. 2) Read about variable scope: stackoverflow.com/questions/500431/… Commented Feb 18, 2015 at 16:11
  • 1
    @SergiuParaschiv: Scope looks fine to me. I suspect it's an asynchronous thing. Commented Feb 18, 2015 at 16:13
  • 1
    @slebetman Would agree, findAll is async, so OP should use the function in async manner. Commented Feb 18, 2015 at 16:15

1 Answer 1

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/');
});
Sign up to request clarification or add additional context in comments.

7 Comments

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
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.
I still get the same error. I have added the exact error message I get in the OP.
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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.