Mongodb驱动程序:主机和选项之间缺少定界斜杠 [英] Mongodb Driver: missing delimiting slash between hosts and options
问题描述
我从 MONGODB NODE.JS DRIVER 2.2 <复制了代码/a>并进行了一些修改.
I copied codes from MONGODB NODE.JS DRIVER 2.2 and modified a little.
// 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 error.
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)
在控制台中提到MongoClient.connect中有问题.
It is mentioned in the console that there is something wrong in MongoClient.connect.
但是,我无法解决此问题.是错误还是我的问题?
However, I couldn't fix this problem. Is it a bug or my problem?
谢谢.
推荐答案
端口号和选项之间缺少/
.
You are missing a /
between the port number and the options.
mongodb://%s:%s@localhost:27017/?authMechanism=%s&authSource=%s
有关连接字符串的更多信息: https://docs.mongodb.com/manual /reference/connection-string/
More information about connection strings: https://docs.mongodb.com/manual/reference/connection-string/
这篇关于Mongodb驱动程序:主机和选项之间缺少定界斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!