Clementine tutorial : "TypeError: db.collection is not a function"

Howdy, campers. I am working through the clementine tutorial (http://www.clementinejs.com/tutorials/tutorial-beginner.html) at the moment (I am using c9.io) when I ran into a bit of a snag.

After doing everything correctly up to about the mid-point of the tutorial, and then double-checking that I had done everything correctly, testing resulted in the following error message: “TypeError: db.collection is not a function”

This actually through me for a loop at first. I could not figure out what I was doing wrong with db.

Turns out, nothing.

I finally came across this solution:

 npm uninstall mongodb --save

 npm install mongodb@2.2.33 --save

Problem was with the library I was using. The above commands worked like a charm and now I no longer get the error.

On to the next bug!

1 Like

Shouldn’t it will be better if you always just run

npm install mongodb --save

Why would u specified version ? If you don’t specify version you will get the latest one. Unless u want specific version. :slight_smile:

Nope, that causes the error!
I just uninstalled and ran npm install mongodb --save, then ran “node server” and got the error.
Then I repeated npm uninstall mongodb --save … npm install mongodb@2.2.33 --save, then ran “node server” and my application is now running.

Something though tells me it would be far better to be able to run on whatever the latest version is. I obviously need a better understanding of why this is happening. :slight_smile:

Well it’s strange. When I run in my git bash npm install mongodb --save it’s installs perfectly :slight_smile:

Yeah it is strange. Tickle on back of neck says being tied to a certain version is going to cause me problems down the road. Do you have any idea what I might research in order to find out why this is happening?

Try to clear cache ?

I just closed all browser sessions, then ran new session, carried out npm uninstall mongodb --save then npm install mongodb --save

then ran the application with “node server” and am still getting the error. And again when I go back and user npm install mongodb@2.2.33 --save then run the application, there is no error.

Application code is here, btw: https://github.com/olddognewtrix123/clementinetutorial1

What error u get when u run npm install mongodb --save ?
If no error, what displays in package.json file after install ?

olddognewtrix123:~/workspace (master) $ node server
MongoDB successfully connected on port 27017.
/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:797
throw err;
^

TypeError: db.collection is not a function
at new clickHandler (/home/ubuntu/workspace/app/controllers/clickHandler.server.js:4:20)
at module.exports (/home/ubuntu/workspace/app/routes/index.js:7:21)
at /home/ubuntu/workspace/server.js:25:2
at args.push (/home/ubuntu/workspace/node_modules/mongodb/lib/utils.js:431:72)
at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:254:5
at connectCallback (/home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:933:5)
at /home/ubuntu/workspace/node_modules/mongodb/lib/mongo_client.js:794:11
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)

{
“name”: “beginner-app”,
“version”: “1.0.0”,
“description”: ",-----.,–.
“main”: “server.js”,
“scripts”: {
“test”: “echo “Error: no test specified” && exit 1”
},
“author”: “”,
“license”: “MIT”,
“dependencies”: {
“express”: “^4.16.2”,
“mongodb”: “^3.0.0-rc0”
}
}

const myDB=db.db(‘clementinejs’);

This is the answer. U need to use const myDB=db.db('clementinejs'); in your case.
Anyway u will not encounter this error when u will use mongoose which is more fluent. :slight_smile:

1 Like

Thanks!!

I updated this (which was pretty much what is exactly in the tutorial)

mongo.connect('mongodb://localhost:27017/clementinejs', function (err, db) {

if (err) {
	throw new Error('Database failed to connect!');
} else {
	console.log('MongoDB successfully connected on port 27017.');
}

app.use('/public', express.static(process.cwd() + '/public'));
app.use('/controllers', express.static(process.cwd() + '/app/controllers'));

routes(app, db);

app.listen(8080, function () {
	console.log('Listening on port 8080...');
});

});

in my server.js file to this:

 mongo.connect('mongodb://localhost:27017/clementinejs', function (err, client) {

    if (err) {
	throw new Error('Database failed to connect!');
    } else {
	console.log('MongoDB successfully connected on port 27017.');
	var myDB=client.db('clementinejs');
    }

    app.use('/public', express.static(process.cwd() + '/public'));
    app.use('/controllers', express.static(process.cwd() + '/app/controllers'));

    routes(app, myDB);

    app.listen(8080, function () {
	    console.log('Listening on port 8080...');
    	});

 });

and I am now no longer getting the error anymore when running ‘node server’

For the record, this is what now appears in my json file:

"dependencies": {
   "express": "^4.16.2",
   "mongodb": "^3.0.0-rc0"
 }

Perfect :slight_smile: Glad to be able to help :slight_smile:

Had the same problem. Still don’t know the problem with new versions, but this solve the issue.
Thanks @olddognewtrix123 !

If you’re using Mongodb 3.0 and above the connect call back no longer returns the database object but rather the client object. To fix this problem you need to grab the database object from the client object like so:

let db = client.db(dbname)

see this post

on stackoverflow