MongoParseError: URI malformed, cannot be parsed (upgraded from 2.1 to 3.7 mongodb nodejs)

Hello, today we upgraded from mongodb driver nodejs (2.1 to 3.7).

Sadly we got this error MongoParseError: URI malformed, cannot be parsed.
I tried many things.

Could you help us given this piece of code please?

'use strict';

var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var swaggerize = require('swaggerize-express');
var path = require('path');
var fs = require('fs.extra');
var cors = require('cors');
var request = require('request-json');

var i18n = require('i18n-future').middleware();
var locale = require("locale");
var ejwt = require('express-jwt');

var gm = require("gm").subClass({
  imageMagick: true
});

var formidable = require('formidable');
var stream = require('readable-stream');
var uuid = require('node-uuid');

var UploadedFile = require(__dirname + '/models/uploadedfile.js');
var Error = require(__dirname + '/models/error.js');

var mongodb = require('mongodb'),
  MongoClient = mongodb.MongoClient;

var sharedData = module.exports.sharedData = {
  credentials: {}
};

if (fs.existsSync(__dirname + '/env.json')) {
  
  var env = JSON.parse(fs.readFileSync(__dirname + '/env.json'));

  for (var key in env) {
    if (!process.env[key]) {
      process.env[key] = env[key];
    }
  }
}

sharedData.credentials.sendgridOption = {
  auth: {
    // api_user: process.env.SENDGRID_USERNAME,
    api_key: process.env.SENDGRID_PASSWORD
  }
};

var tools = sharedData.tools = require('./tools.js');
var emailer = sharedData.emailer = require(__dirname + '/emailer/index.js');
var statics = sharedData.statics = require(__dirname + '/statics/index.js');
var cronJobs = require(__dirname + '/cronJobs.js');

// Start Express
var app = express();
var server = http.createServer(app);

// ########################
// # Defining middlewares #
// ########################
// CORS
app.use(cors());
// Provide swagger access on /api route

app.use('/', function (req, res, next) {
  // Mobile redirection
  // var MobileDetect = require('mobile-detect');
  // if (req.headers && req.headers['user-agent'] && req.path === '/') {
  //   var md = new MobileDetect(req.headers['user-agent']);

  //   if (md.phone()) {
  //     return res.redirect('/mobile');
  //   }
  // }
  next();
});

app.use('/api', express.static(__dirname + '/public/swagger'));

app.use('/api/files', function (req, res, next) {
  var path = req.path;
  if (req.path.substr(0, 1) !== '/') {
    path = '/' + path;
  }
  res.redirect(process.env.GDM_FILES_ENDPOINT + path);
});


app.use('/config/swagger.json', express.static(__dirname + '/config/swagger.json'));

// JWT Token
app.use(ejwt({
  secret: process.env.JWT_SECRET
}).unless({
  path: [
    '/favicon.ico',
    '/api/sitemap.xml',
    '/api/ping',
    '/api/register/mobile',
    '/api/register/web',
    '/api/auth/mobile',
    '/api/auth/web',
    '/api/auth/regenerate',
    '/api/admin',
    '/api/public',
    '/api/devis',
    /^\/api\/mobile\/.*/,
    /^\/api\/admin\/.*/,
    /^\/api\/public\/.*/,
    /^\/api\/devis\/.*/
  ]
}));
// i18n
app.use(i18n);
// Middleware to get the local of users
app.use(locale(['en', 'en_US', 'fr', 'fr_FR']));
// Url encoded
app.use(bodyParser.urlencoded({
  extended: true
}));



// JSON Body parser
app.use(bodyParser.json());

app.post('/api/file', function (req, res) {
  var form;
  form = new formidable.IncomingForm();

  form.onPart = function (part) {
    var outStream, path = '';

    if (part.filename == null) {
      return form.handlePart(part);
    }
    

    outStream = new stream.PassThrough();
    part.on('data', function (buffer) {
      form.pause();
      return outStream.write(buffer, function () {
        return form.resume();
      });
    });
    part.on('end', function () {
      form.pause();
      return outStream.end(function () {
        return form.resume();
      });
    });


    // Generate time-based filename;
    var uploadedFile = new UploadedFile({
      originalFileName: part.filename,
      filename: uuid.v1() + '.' + part.filename.split('.').pop()
    });

    if (req.get('fileName')) {
      uploadedFile.filename = req.get('fileName');
    }

    if (req.get('path')) {
      if (req.get('path').charAt(0) !== '/') {
        path += '/';
      }
      path += req.get('path');
    } else {
      path += '/';
    }

    path += uploadedFile.filename;
    console.log("received part: " + part.filename + ", uploading to OVH at: " + path);

    outStream.pipe(sharedData.tools.uploadFile(path, function (success, data) {
      console.log(success, data)

      if (success === false) {
        var error = new Error();
        error.code = 400;
        error.message = req.translate('error.unexpected');
        error.data = data;

        res.status(error.code).send(error);
        return;
      }

      return res.jsonp(uploadedFile);
    }));
  };
  form.on('error', function (err) {
    console.log(err);
    var error = new Error();
    error.code = 400;
    error.message = req.translate('error.unexpected');
    error.data = err;

    res.status(error.code).send(error);
  });
  form.on('end', function () {
    console.log('form end');
  });

  form.parse(req);
});

// Connect to MONGO DB
MongoClient.connect(process.env.MONGODB_URL, {
    mongos: {
      ssl: true,
      sslValidate: false,
      poolSize: 2,
      "socketOptions": {
        "keepAlive": 120
      }
    },
    "server": {
      "socketOptions": {
        "autoReconnect": true,
        "keepAlive": 120
      }
    }
  },
  function (err, db) {
    if (err) {
      console.log(err);
    } else {
      sharedData.mongo = {
        db: db,
        oid: mongodb.ObjectID
      };

      // Create this object for compatibility with old api
      sharedData.manager = {
        app: app,
        server: server
      };

      require('./handlers/manager/server.js');
      // Initialize routes with swagger document
      app.use(swaggerize({
        api: path.resolve('./config/swagger.json'),
        handlers: path.resolve('./handlers')
      }));

      server.listen(process.env.PORT || 8022, function () {
        console.log((new Date()) + " > Server ready to accept requests on port " + (process.env.PORT || 8022));

        // start cronJobs
        cronJobs.init();
      });
    }
  }
);

and here is how is formatted my connection string in the env.json file :
“MONGODB_URL”:“mongodb+srv://:@sl-eu-lon-2-portal.1.dblayer.com:10096/gdm?ssl=true&retryWrites=true&w=majority”

@Gu_L,

Given the error being thrown is MongoParseError: URI malformed, cannot be parsed the issue appears to be with the connection string.

The connection string you’ve shared is mongodb+srv://:@sl-eu-lon-2-portal.1.dblayer.com:10096/gdm?ssl=true&retryWrites=true&w=majority which has the following issues:

  1. You’ve requested a mongodb+srv connection, but also specified a port. If this isn’t an SRV connection (which it doesn’t appear to be) change this to mongodb://
  2. You’ve provided an empty username and password. Perhaps this was a copy/paste error? (mongodb+srv://:@ should be mongodb+srv://<user>:<pass>@

Addressing the above should make the URI parseable by the Node.js driver.

Hello and thanks for your time,
I put empty user and pass as it is sensitive information, im 100% sure with my logins, and i tried with and without +srv in the connection string.
We just updated the mongodb driver thats it. (because of an error of deprecation we updated from 2.1.7 to 3.7). The string didnt change.
Thanks in advance.