Mongodb driver: missing a slash between hosts and options

I copied the codes from MONGODB NODE.JS DRIVER 2.2 and changed a bit.

// connect to mongodb var MongoClient = require('mongodb').MongoClient, f = require('util').format; var user = encodeURIComponent('admin'), password = encodeURIComponent('123456'), authMechanism = 'DEFAULT', authSource = 'admin'; // connection url var url = f('mongodb://%s:% s@localhost :27017?authMechanism=%s&authSource=%s', user, password, authMechanism, authSource); var db = null; MongoClient.connect(url, function(err, db) { //Here is line 20! if(err) { console.log('Unable to connect to the mongoDB server. Error:', err); } else { console.log('Connection established to', url); db = database // once connected, assign the connection to the global variable } }) 

However, I met an odd mistake.

 Error: missing delimiting slash between hosts and options. at module.exports (/home/lixing/Dropbox/thesis/node_modules/mongodb/lib/url_parser.js:37:11) at connect (/home/lixing/Dropbox/thesis/node_modules/mongodb/lib/mongo_client.js:289:16) at Function.MongoClient.connect (/home/lixing/Dropbox/thesis/node_modules/mongodb/lib/mongo_client.js:113:3) at Object.<anonymous> (/home/lixing/Dropbox/thesis/server.js:20:13) at Module._compile (module.js:570:32) at Object.Module._extensions..js (module.js:579:10) at Module.load (module.js:487:32) at tryModuleLoad (module.js:446:12) at Function.Module._load (module.js:438:3) at Module.runMain (module.js:604:10) 

The console mentions that something is wrong with MongoClient.connect.

However, I could not solve this problem. Is this a mistake or my problem?

Thanks.

+5
source share
1 answer

You are missing / between the port number and parameters.

mongodb://%s:% s@localhost :27017/?authMechanism=%s&authSource=%s

Additional information about connection strings: https://docs.mongodb.com/manual/reference/connection-string/

+9
source

Source: https://habr.com/ru/post/1264093/


All Articles