Node.js和mongodb TypeError:undefined不是函数

时间:2014-02-12 11:01:58

标签: javascript node.js mongodb

我是node.js和mongodb的新手。我正在使用express和jade作为我正在写的测试应用程序。我已经配置了mongodb和node.js但是当我尝试从数据库中检索数据时,我收到以下错误: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)

这是我的代码: 这是负责获取数据的用户列表路由。

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

这是userlist.jade文件:

extends layout

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

以下是我称之为userlist的代码,即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'));
});

1 个答案:

答案 0 :(得分:1)

如果您尝试修改find方法

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