Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Web Technology by (50.2k points)

With the help of homebrew, I have installed MOngoDB and it is working fine for me. My both mongo and mongod are running in the background.

I've also verified that I can reach the web interface at localhost:28017.

After verifying I installed node.js and install the mongoose package. I then tried to connect using node.js app:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

After writing the above command I get the following error:

events.js:72

        throw er; // Unhandled 'error' event

              ^

Error: connect ECONNREFUSED

    at errnoException (net.js:901:11)

    at Object.afterConnect [as oncomplete] (net.js:892:19)

Am I missing something?

1 Answer

0 votes
by (108k points)
edited by

I think you are adding some other lines in your node.js code that is not required in your application.

There is some code having wrong database information which is causing to call a separate database. For setting up the connection, refer to the following code:

var mongoose = require('mongoose');

var mongoURI = "mongodb://localhost:27017/test";

var MongoDB = mongoose.connect(mongoURI).connection;

MongoDB.on('error', function(err) { console.log(err.message); });

MongoDB.once('open', function() {

  console.log("mongodb connection open");

});

If you are a beginner and want to know more about MongoDB then refer to the following MongoDB tutorial which will help you to learn MongoDB from scratch.

Also, have a look at this professional Full Stack Developer Training, designed to enhance your skills in the field.

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Mar 15, 2020 by ashely (50.2k points)

Browse Categories

...