任何人都知道为什么在使用猫鼬时我总是在控制台日志中得到这个
the options [port] is not supported
快速上下文:
mongod
const mongoProfile = require('../config/mongo-profile.json');
let mongooseConnPromise = mongoose.connect(mongoProfile.url, mongoProfile.connectionOptions);
let dbConn = mongoose.connection;
dbConn.on('error', console.error.bind(console, 'connection error:'));
dbConn.on('open', () => {
RpDataModel.find({$or: queryData},
(err, docs) => {
dbConn.close();
res.append('ETag', `My PerfTest, ${new Date(Date.now())}`);
res.json(docs);
});
});
“('../ config / mongo-profile.json')” ?conn str和下面的选项
{
"url": "mongodb://localhost",
"connectionOptions": {
"useNewUrlParser": true,
"port": 27017,
"user": "rpTest",
"dbName":"rp-db-perf-test",
"pass":"...",
"keepAlive": true,
"keepAliveInitialDelay": 0
}
}
答案 0 :(得分:0)
我不是猫鼬专家,但是根据文档,port
不是有效的选择:
Mongoose使用Node.js MongoDB驱动程序。这是available connection options的列表。
根据猫鼬文档,正确的连接方式是使用MongoDB Standard Connection String Format,即:
mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[database][?options]]
您可以在Mongoose的Connections文档中看到一些示例。
据我了解,您可以将mongo-profile.json
更改为:
{
"url": "mongodb://localhost",
"port": 27017,
"user": "rpTest",
"dbName":"rp-db-perf-test",
"pass":"...",
"connectionOptions": {
"useNewUrlParser": true,
"keepAlive": true,
"keepAliveInitialDelay": 0
}
}
您的连接代码为:
const mongoProfile = require('../config/mongo-profile.json');
var url = mongoProfile.url;
var port = mongoProfile.port;
var dbName = mongoProfile.dbName;
var user = mongoProfile.user;
var pass = mongoProfile.pass;
let mongooseConnPromise = mongoose.connect(
'mongodb://' + user + ':' + pass + '@' + url + ':' + port + '/' + dbName,
mongoProfile.connectionOptions
);
let dbConn = mongoose.connection;
dbConn.on('error', console.error.bind(console, 'connection error:'));
答案 1 :(得分:0)
根据文档,即使您使用的是最旧版本的猫鼬(3.8),也无法指定类似的端口。根据文档的唯一方法是像这样在连接字符串中传递它:
mongoose.connect('mongodb://host:port');
但是由于您使用的是默认端口为localhost,因此您甚至不需要在此指定端口:
mongoose.connect('mongodb://host');
因此,只需从配置文件中删除参数即可,一切都很好。