尝试运行Node.js应用程序时出现TypeError错误

编译我的Node.js应用程序时,我不断收到第13行的这个错误:

var db = mc.db('course'); ^ TypeError: undefined is not a function at Object.<anonymous> (/app.js:13:13) at Module._compile (module.js:460:26) at Object.Module._extensions..js (module.js:478:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:501:10) at startup (node.js:129:16) at node.js:814:3 

这里是代码:

 var express = require ('express'), app = express(), cons = require('consolidate'), MongoClient = require('mongodb').MongoClient; Server = require('mongodb').Server; app.engine('html',cons.swig); app.set('view engine','html'); app.set('views', __dirname + "/views"); var mc = new MongoClient(new Server('localhost',27017,{'native_parser' : true })); var db = mc.db('course'); app.get('/',function(req,res){ dbs.collection('listCourse').findOne({}, function (err, doc){ res.render('hello',doc); }); }); app.get('*',function(req,res){ res.send('Page not found',404); }); mc.open(function(err,mc){ if(err) throw err; app.listen(8080); console.log('expres server started on port 8080'); }); 

你们能告诉我我哪里出了错?

       

网上收集的解决方案 "尝试运行Node.js应用程序时出现TypeError错误"

在声明var mc后添加这个:

 console.log(mc); 

你会看到它返回一个包含Db()函数的connect对象。

所以你可以尝试改变你的代码:

 var db = mc.connect.Db('course'); 

MongoClient文档( https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html )显示了用于连接的代码:

 var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, BSON = require('mongodb').pure().BSON, assert = require('assert'); // Connect using the connection string MongoClient.connect("mongodb://localhost:27017/integration_tests", {native_parser:true}, function(err, db) { assert.equal(null, err); db.collection('mongoclient_test').update({a:1}, {b:1}, {upsert:true}, function(err, result) { assert.equal(null, err); assert.equal(1, result); db.close(); }); }); 

所以你可以适应你的代码。 就像是:

  var express = require ('express'), app = express(), cons = require('consolidate'), MongoClient = require('mongodb').MongoClient; Server = require('mongodb').Server; app.engine('html',cons.swig); app.set('view engine','html'); app.set('views', __dirname + "/views"); MongoClient.connect("mongodb://localhost:27017/course", {native_parser:true}, function(err, db) { if(err) throw err; app.get('/',function(req,res){ db.collection('listCourse').findOne({}, function (err, doc){ res.render('hello',doc); }); }); app.get('*',function(req,res){ res.send('Page not found',404); }); db.close(); }); app.listen(8080); console.log('expres server started on port 8080'); 

MongoClient实例没有'db'方法。

build议你连接哟数据库这样的东西:

 var mongojs = require("mongojs"); var collections = ['users', 'comments', 'books']; var db = mongojs( "MyDBname", collections); db.users.find({_id: "abc1234567547abc1c3c35"}, function(err, users){ // do something }) 

祝你好运