SalmaFG
SalmaFG

Reputation: 2202

Mongoose connection not working

I have been trying to set up a mongoose connection for my Node.js app and so far, it is not working and I have no idea what I'm doing wrong. I have followed the documentation and several tutorials but when I print mongoose.connection.readyState, I get 0 which means I'm disconnected. Also, whenever I try a route, I don't get any response and I get "Connection timed out" error.

I am sure it is not an authentication problem because I have tried to access the DB directly from the terminal using the same URL and credentials and they work.

Here's my code:

app.js:

var express = require('express');
var mongoose = require('mongoose');
var passport = require('passport');
var session  = require('express-session');
var MongoStore = require('connect-mongo')(session);

var app = express();

app.use('/', routes);

mongoose.Promise = global.Promise;
var db = mongoose.createConnection('mongodb://..', {user, pass});
var store = new MongoStore({
  mongooseConnection: db
});

app.use(session({
  key: 'key',
  secret: 'secret',
  store:store,
  proxy: true,
  resave: true,
  saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
console.log(mongoose.connection.readyState);

module.exports = app;

bin/www:

var app = require('../app');
var debug = require('debug')('tableping-backend:server');
var http = require('http');

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

var server = http.createServer(app);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    return val;
  }

  if (port >= 0) {
    return port;
  }

  return false;
}

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

and this is what I get in the console:

$ npm start
> node ./bin/www

0

Upvotes: 1

Views: 1863

Answers (1)

Aruna
Aruna

Reputation: 12022

You can try the credentials like this mongoose.createConnection('mongodb://username:password@host:port/database') as below,

var db = mongoose.createConnection('mongodb://' + user + ':' + pass + '..');

Also, you can change this to mongoose.connect instead of mongoose.createConnection as below,

var db = mongoose.connect('mongodb://username:password@host:port/d‌​ataba‌​se');
var store = new MongoStore({
   mongooseConnection: db.connection
});

Upvotes: 1

Related Questions