使用 Mongodb nodejs 驱动程序版本 4 更新到 Mongoose 6 时,出现以下错误:
MongoInvalidArgumentError: No AuthProvider for DEFAULT defined.
at prepareHandshakeDocument (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:153:29)
at performInitialHandshake (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:63:5)
at /home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:25:9
at callback (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:244:9)
at Socket.connectHandler (/home/arch/git/project/node_modules/mongodb/lib/cmap/connect.js:282:9)
at Object.onceWrapper (events.js:519:28)
at Socket.emit (events.js:400:28)
at Socket.emit (domain.js:470:12)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1134:10)
From previous event:
at NativeConnection.Connection.openUri (/home/arch/git/project/node_modules/mongoose/lib/connection.js:778:19)
at /home/arch/git/project/node_modules/mongoose/lib/index.js:330:10
at promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/helpers/promiseOrCallback.js:10:12)
at Mongoose._promiseOrCallback (/home/arch/git/project/node_modules/mongoose/lib/index.js:1151:10)
at Mongoose.connect (/home/arch/git/project/node_modules/mongoose/lib/index.js:329:20)
at connectToDb (/home/arch/git/project/core/configs/database.js:101:8)
at createServer (/home/arch/git/project/app.js:65:16)
at Object.<anonymous> (/home/arch/git/project/app.js:262:3)
at Module._compile (internal/modules/cjs/loader.js:1072:14)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
at Module.load (internal/modules/cjs/loader.js:937:32)
at Function.Module._load (internal/modules/cjs/loader.js:778:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47
我没有设置任何身份验证(这是我的本地开发环境),所以我不明白问题出在哪里。以下是相关的连接代码:
let connectionURL = 'mongodb://';
const connectionOptions = {
authSource: options.authSource,
};
if (options.user && options.password) {
connectionURL += `${options.user}:${options.password}@`;
}
connectionURL += `${options.host}/${dbName}?authSource=${options.authSource}`;
mongoose
.connect(connectionURL, connectionOptions, function onConnected(err) {
if (err) {
return cb(new Error(`Can not connect to database ${dbName}`));
}
return cb(null, mongoose.connection);
});