Node.js, mongodb returning a string representation of a function, not the output

I am new to the world of mongodb and node.js . I have a project in which I put the mongodb code in a routes, and I require it in my server.js.

now in that module I have one method that will return all entries in one collection ( it works ).

I am trying to call that function from the server.js file, but I usually end up with the response printing out the function, not executing it and returning the output!!

Example :

  var http = require('http'),
  location = require('./routes/locations');
  http.createServer(function (request, response) {
     response.writeHead(200, {'Content-Type': 'text/plain'});
     response.write(location.findAll() + '');
     response.end();
 }).listen(8080);

Now when I direct my UI to 8080, I want to get the output of location.findall, instead I get an undefined message, and in node the following exception :

  TypeError: Cannot call method 'send' of undefined

I know this is probably a newbie question, I am coming from java, .NET, and iOS world. sorry!!

update : To clarify more, here is what I have in routes/locations.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('locationsdb', server);
 db.open(function(err, db) {
     // initlization code    
  });

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

  • You need to actually call the function!
  • I'll guess findAll is async, so you should use the function in async manner

I don't know what is in your route/locations file, but it probably should look like this:

var http = require('http'),
location = require('./routes/locations');
http.createServer(function (request, response) {
    location.findAll(function(err, locations) {
        response.writeHead(200, {'Content-Type': 'text/plain'});
        response.write(locations);
        response.end();
    });
}).listen(8080);

I'm not sure, but try

response.write(location.findAll() + '');