1

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
Aruna
  • 11,959
  • 3
  • 28
  • 42
SalmaFG
  • 2,022
  • 3
  • 23
  • 36
  • Possible duplicate of [(node:3341) DeprecationWarning: Mongoose: mpromise](http://stackoverflow.com/questions/38138445/node3341-deprecationwarning-mongoose-mpromise) – deChristo Dec 20 '16 at 11:11
  • @deChristo I added `mongoose.Promise = global.Promise;` as suggested in the answer and it eliminated the warning. But I still have the same problem. – SalmaFG Dec 20 '16 at 11:16

1 Answers1

1

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
});
Aruna
  • 11,959
  • 3
  • 28
  • 42
  • Tried that but it didn't fix the problem too. – SalmaFG Dec 20 '16 at 11:34
  • Can you post the connection in the same format like, `'mongodb://username:password@host:port/database'` instead of `'..'`. You can mock the sensitive item. – Aruna Dec 20 '16 at 11:39
  • `mongoose.createConnection('mongodb://username:password@ds139428.mlab.com:39428/database');` – SalmaFG Dec 20 '16 at 11:45
  • I even tried entering a wrong password and I got `MongoError: Authentication failed.` so clearly it is authenticating correctly but for some reason, the connection doesn't open. – SalmaFG Dec 20 '16 at 11:48
  • Can you try `mongoose.connect('mongodb://username:password@ds139‌​428.mlab.com:39428/d‌​atabase');` instead of `createConnection` – Aruna Dec 20 '16 at 11:51
  • Now getting this error: `TypeError: options.mongooseConnection.once is not a function at MongoStore` pointing to the line `var store = new MongoStore({ mongooseConnection: db });` – SalmaFG Dec 20 '16 at 11:53
  • Yes, you have to change this to `var store = new MongoStore({ db: db.connection.db });` – Aruna Dec 20 '16 at 12:03
  • It's working now! :) But using with `var store = new MongoStore({ db: db.connection });`. Thanks so much for your help! – SalmaFG Dec 20 '16 at 12:23
  • I will update the same in the answer and you can accept it :-) – Aruna Dec 20 '16 at 12:25
  • Sorry I meant `var store = new MongoStore({ mongooseConnection: db.connection })`! That's the correct line. Yeah, do that please :) – SalmaFG Dec 20 '16 at 12:31
  • @SalmaFG Sure, Done :-) – Aruna Dec 20 '16 at 12:33