Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a REST API With Express Modeling Data for the API Connecting Mongoose to MongoDB

[SOLVED] TypeError: db.on is not a function

Hey,

I am receiving the following error when I run 'node mongoose_sandbox.js' in terminal.

db.on("error", function(err){
   ^

TypeError: db.on is not a function
    at Object.<anonymous> (/Users/johnshockey/Documents/Treehouse/qa-rest-api/mongoose_sandbox.js:9:4)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:141:18)
    at node.js:933:3

I followed the docs as well and I still get the same error. http://mongoosejs.com/docs/

Yes, everything was installed correctly.

It will help if you post your mongoose_sandbox.js file as well?

In the video you are instructed to call:

db.on('error', function(err){
    console.error("connection error;", error);
});

error actually needs to be err:

db.on('error', function(err){
    console.error("connection error;", err);
});

*also, the video doesn't mention that you need to start the database before you can run your test. But you need to.

In order to start the mongodb database open the terminal in your project folder and write:

mongod

Than run your:

node mongoose_sandbox.js

1 Answer

Thanks Jonathan,

I went to the actual mongoose docs and did end up changing it from 'error' to 'err,' however, that still didn't solve my issue.

But the following change finally resolved the error:

var db = mongoose.connection;

I changed 'mongoose.connect' to 'mongoose.connection'