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

时间:2015-05-29 13:12:57

标签: javascript node.js mongodb express

编译我的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');
});

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

2 个答案:

答案 0 :(得分:1)

在声明var mc

后添加此内容
console.log(mc);

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

因此,您可以尝试将代码更改为:

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');

答案 1 :(得分:0)

MongoClient实例没有&#39; db&#39;方法

建议你连接yo db这样的东西:

var mongojs = require("mongojs");
var collections = ['users', 'comments', 'books'];
var db = mongojs( "MyDBname", collections);

db.users.find({_id: "abc1234567547abc1c3c35"}, function(err, users){
    // do something
})
祝你好运