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.
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.
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?
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.
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)
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.
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:
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: