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?