2014-02-12 78 views
4

我是node.js和mongodb的新手。我正在使用express和jade來測試我正在編寫的應用程序。我已經配置的MongoDB和node.js的,但是當我嘗試和檢索數據從數據庫中,我得到以下錯誤:TypeError: undefined is not a functionNode.js和mongodb TypeError:undefined不是函數

/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 

下面是代碼,我調用用戶列表即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')); 
}); 
+0

你可以發佈鱈魚你在哪裏調用'userlist(...)'? – heinob

+0

heinob,現在就看看吧...... –

+0

老問題,但是我嘗試使用findAndModify時遇到過類似的問題。各種驅動程序之間的困惑,我忘了傳遞迴調方法,導致這個錯誤被拋出。在類似的路線上,可以嘗試查找方法的「唯一回調」語法。 – NikhilWanpal

回答

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 
     }); 
    }); 
    }; 
}; 
+0

沒有工作,我也是新的node.js和具有相同的問題。在實施你的解決方案時,我得到錯誤對象#沒有方法'toArray' – Ray

+0

你能顯示引發該錯誤的示例代碼嗎? – ppoliani

相關問題