vadimdemedes / mongorito

🍹 MongoDB ODM for Node.js apps based on Redux
1.38k stars 90 forks source link

Could not connect to replica set #132

Open oliveirajota opened 8 years ago

oliveirajota commented 8 years ago

I've receiving this error when trying to connect to master and replica together. "Illegal trailing backslash after database name"

let uri = "mongodb://user:pass@host:port/database";
let uriReplica = "mongodb://user:pass@host:port/database";

let connection =  Mongorito.connect(uri, uriReplica);
vadimdemedes commented 8 years ago

Could you post exact code (without login & passwords, ofc)?

oliveirajota commented 8 years ago

The problem was adding the database string on the master url and the "mongodb://" string on the replica set url.

The solution was mount a single url string with all the correct parameters as described on the MongoDB Documentation found here.

Also I've passed the replica set name and slaveOk=true on the options parameters.

This is the working code:

import mongorito from 'mongorito';
import util from 'util';

const uri = util.format(
    'mongodb://%s:%s@%s:%s',
    'user',
    'pass',
    'host',
    'port'
);

const uriReplica = util.format(
    '%s:%s/%s?slaveOk=true&replicaSet=%s',
    'host_replica',
    'port',
    'database',
    'replicaName'
);

const url = util.format('%s,%s', uri, uriReplica);
// url = 'mongodb://user:pass@host:port,host_replica:port/database?slaveOk=true&replicaSet=replicaName'

export default mongorito.connect(url);

Mongorito was adding the 'mongodb://' string even if I doesn't pass it on the second url.