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 Building a MEAN Application Setting Up MongoDB Using MongoDB with Express and Mongoose

Akshit Jindal
Akshit Jindal
1,341 Points

Failed Mongodb Connection?

I am doing the same thing as done in the vedio but i am getting failed mongodb connection.

database.js

'use strict';

var mongoose=require('mongoose');

mongoose.connect('mongodb://localhost/mean-todo',{useMongoClient:true},function(err){ if(err) { console.log('Failed mongodb conection!'); } else { console.log('Successful mongodb conection!'); } });

Have you tried to do:

mongoose.connect('mongodb://localhost:27017/mean-todo')
const db = mongoose.connection;
db.on("connection" ()=>{
        console.log('connected');
});
db.on("error", ()=>{
         console.log('error');
});

Or use your approach with the callback, but you may need to provide the port number which is 27017 (default)

Akshit Jindal
Akshit Jindal
1,341 Points

Even after using this solution, error is coming.

Are you starting the mongodb server with mongod in a seperate terminal window/tab?