kodokbuncit
kodokbuncit

Reputation: 43

TypeError when attempting to run Node.js application

I keep getting this error on line 13 when compiling my Node.js app:

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

Here is the code:

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

Can you guys tell me where I've gone wrong?

Upvotes: 2

Views: 565

Answers (2)

Mark Leiber
Mark Leiber

Reputation: 3138

Add this after you declare var mc:

console.log(mc);

You'll see that it returns a connect object which contains a Db() function.

So you can try changing your code to:

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

The MongoClient documentation (https://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html) shows this code for connecting:

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();
    });
  });

So you can adapt this to your code. Something like:

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

Upvotes: 1

siavolt
siavolt

Reputation: 7067

MongoClient instance dont have 'db' method.

Recommend you connect yo db somthing like this:

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

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

Good luck

Upvotes: 0

Related Questions