4

I am new to node.js and mongodb. I am using express and jade for a test app i am writing. I have configured mongodb and node.js but when I try and retrieve data from the database, I get the following error: TypeError: undefined is not a function

/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/base.js:242
            throw message;      
                  ^
    TypeError: undefined is not a function
        at commandHandler (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/cursor.js:706:16)
        at /Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/db.js:1806:9
        at Server.Base._callHandler (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/base.js:442:41)
        at /Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/server.js:485:18
        at MongoReply.parseBody (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
        at null.<anonymous> (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/server.js:443:20)
        at EventEmitter.emit (events.js:95:17)
        at null.<anonymous> (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:191:13)
        at EventEmitter.emit (events.js:98:17)
        at Socket.<anonymous> (/Users/apple/Documents/Nodejs/NODE/express_example/node_modules/mongodb/lib/mongodb/connection/connection.js:418:22)

Here is my code: This is the userlist route that is responsible for fetching the data.

exports.userlist = function(db) {
    return function(req, res) {
        var collection = db.get('usercollection');
        collection.find({},{},function(e,docs){
            res.render('userlist', {
                "userlist" : docs
            });
        });
    };
};

Here is the userlist.jade file:

extends layout

block content
    h1.
        User List
    ul
        each user, i in userlist
            li
                a(href="mailto:#{user.email}")= user.username

Here is the code where I call userlist i.e app.js

/**
 * Module dependencies.
 */

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var mongo = require('mongodb');
var monk = require('monk');
var db = monk('localhost:27017/nodetest1');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

// development only
if ('development' == app.get('env')) {
  app.use(express.errorHandler());
}

app.get('/', routes.index);
app.get('/users', user.list);
app.get('/helloworld', routes.helloworld);
app.get('/userlist', routes.userlist(db));

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
4
  • Can you post the code in which you call userlist(...)? Commented Feb 12, 2014 at 11:05
  • heinob, just check it out now... Commented Feb 12, 2014 at 11:13
  • Old question, but I experienced something similar when trying to use findAndModify. Confused between various drivers, I forgot to pass the callback method, that resulted in this error being thrown. On similar lines, one can try the "only callback" syntax for find method. Commented Jul 11, 2014 at 10:04
  • upgrade mongodb node module package to 1.4.28+ inside mongoose package or upgrade mongoose package to 3.8.22 + or you can downgrade your mongodb to 2.7 or lower version. any of those should work Commented Mar 13, 2015 at 13:42

1 Answer 1

1

What if you try to modify the find method

exports.userlist = function(db) {
 return function(req, res) {
     var collection = db.get('usercollection');
     collection.find().toArray(function(e,docs){
        res.render('userlist', {
            "userlist" : docs
        });
     });
   };
};
Sign up to request clarification or add additional context in comments.

2 Comments

did not work, i also new to node.js and having the same problem. While implementing your solution i am getting error Object#<Promise> has no method 'toArray'
Can you show the sample code that throw that error?

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.