Fail to parse Mongo URI on connect #1347

Closed
doomhz opened this Issue Feb 16, 2013 · 0 comments

Comments

Projects
None yet
2 participants

doomhz commented Feb 16, 2013

It didn't work for me to pass a mongo_uri string to the Mongoose (3.5.6) connect method.

We recently switched JSLogger to MongoHQ and I had to specify a new remote Mongo connection URL (mongodb://<user>:<password>@<mongohqhost>:<mongohqport>/<database>) and connect with

mongoose.connect(mongoUri)

Seems that Mongoose always falls back to the default Mongo port (27017). This is the error I got from Mongoose on connect:

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: failed to connect to [jslogger:27017]
    at Server.connect.connectionPool.on.server._serverState (/Users/dumitru/RubymineProjects/jslogger.com/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:564:74)
    at EventEmitter.emit (events.js:115:20)
    at connection.on._self._poolState (/Users/dumitru/RubymineProjects/jslogger.com/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:124:15)
    at EventEmitter.emit (events.js:91:17)
    at Socket.errorHandler (/Users/dumitru/RubymineProjects/jslogger.com/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:465:10)
    at Socket.EventEmitter.emit (events.js:88:17)
    at Socket.connect.require.lookup.addressType (net.js:697:16)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)

While looking into the Mongoose source at the Connection.prototype.open = function (host, database, port, options, callback) function, I found out that the URL is not correctly parsed by the muri library. It always thinks that the Mongo user is the database name and the password is the port (that is non-numeric) and ignores it, falling back to the default Mongo port. That happens only when you have a # in your username or password :) Created an issue here: https://github.com/aheckmann/muri/issues/3.

An workaround is to specify your Mongo credentials as an object and call the mongoose.connect method with the native driver params:

    "mongo": {
      "host": "host",
      "port": "port",
      "user": "user",
      "pass": "pass",
      "db": "dbname"
    }

and load them like this:

var fs = require('fs');
exports = mongoose = require('mongoose');
var environment = process.env.NODE_ENV || 'development';
var options = JSON.parse( fs.readFileSync(process.cwd()+'/config.json', encoding='utf8') )[environment];
var mongoUri = options.mongo_uri;
var mongo = options.mongo;
mongoUri ? mongoose.connect(mongoUri) : mongoose.connect(mongo.host, mongo.db, mongo.port, {user: mongo.user, pass: mongo.pass});
exports = Schema = mongoose.Schema;

@aheckmann aheckmann added a commit that referenced this issue Feb 19, 2013

@aheckmann aheckmann updated; muri to 0.3.1
fixes #1347
36e10e6

aheckmann closed this in 6eca8e1 Feb 19, 2013

@aheckmann aheckmann added a commit that referenced this issue Mar 2, 2013

@aheckmann aheckmann updated; muri to 0.3.1
fixes #1347
5387ad4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment