williamkapke / mongo-mock

Let's pretend we have a real MongoDB
MIT License
240 stars 74 forks source link

TypeError: Cannot read property 'replace' of null (MongoClient.connect) #92

Open glassdimly opened 5 years ago

glassdimly commented 5 years ago

This is our code that we were using to connect to the mongo-mock, which works find in mongodb:

MongoClient.connect(process.env.MONGODB_CONFIG)
         |             ^
      63 |   .then(client => {
      64 |     mongo = client.db('myDb');

The problem was that mongo was depending on us passing in a dbname as the path parameter of the URL:

MongoClient.connect = function(url, options, callback) {
...
  url = urlparse(url);
...
  var dbname = url.pathname.replace(/^\//, '');
  return new Db(dbname, server).open(callback);

...We were not passing in dbname.

The workaround was this:

MongoClient.connect(process.env.MONGODB_CONFIG + '/myDb')
.then(client => {
  mongo = client;

...That is, I needed to send the db in as a string.