Error connecting MongoDB

0

I'm having trouble connecting MongoDB.

Follow 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' }
    
asked by anonymous 28.04.2015 / 10:52

1 answer

1

Normally this error can have several causes, namely:

  • the port is not the default (27017)
  • the mongo server may be protected and the user must be authenticated
  • the server is not running
  • BD does not exist

By now, ; is missing from line var db_string= 'mongodb://localhost/teste' (not mandatory, but always the most "tidy" code)

    
28.04.2015 / 11:15