Error while connecting Mongodb

Asked

Viewed 1,869 times

0

I’m having trouble connecting to Mongodb.

Follows the code:


var express= require('express');

var app= express();

var bodyParser= require('body-parser');

var db_string= 'mongodb://localhost/teste'
var mongoose= require('mongoose');
var db= mongoose.connection;
mongoose.connect(db_string);
db.on('error', console.error.bind(console, 'Erro ao conectar no banco'));
db.once('open', function(){
    var testeSchema= mongoose.Schema({
        name: String,

    });
});

Error displayed on console:


Erro ao conectar no banco { [MongoError: connect ECONNREFUSED] name: 'MongoError', message: 'connect ECONNREFUSED' }

1 answer

1


As a rule this error can have several causes, namely:

  • port is not default(27017)
  • the Mongo server can be protected and the user must be authenticated
  • the server is not running
  • the comic book doesn’t exist

By the way, the ; on the line var db_string= 'mongodb://localhost/teste' (is not mandatory, but always gets the code "tidier")

  • You can show me improvements to fix the problem with these solutions?

  • I tried to test run a simple command from the terminal: show dbs and got the following return: Error: couldn’t connect to server 127.0.0.1:27017 at src/Mongo/shell/Mongo.js:145 Exception: connect failed

  • You executed the server command (mongod) 1st?

  • I was having trouble installing Mongodb, reinstalled it and the problem was solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.