2

使用 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);
  });
4

1 回答 1

2

对于无密码/无用户连接,请确保连接 url 不包含authSource对我有用。这在驱动程序版本 4 之前以某种方式工作,但不再有效。

此外,要么选择在对象中传递选项,要么在连接字符串中传递选项。正如文档所说:

最佳实践是将开发和生产之间可能不同的选项(如 replicaSet 或 ssl)放在连接字符串中,并将应保持不变的选项(如 connectTimeoutMS 或 poolSize)放在选项对象中

const connectionOptions = {};

if (options.user && options.password) {
  connectionOptions.user = options.user;
  connectionOptions.password = options.password;
  connectionOptions.authSource = options.authSource;
}

const connectionURL = `mongodb://${options.host}/${dbName}`;

mongoose
  .connect(connectionURL, connectionOptions, function onConnected(err) {
    if (err) {
      return cb(new Error(`Can not connect to database ${dbName}`));
    }

    return cb(null, mongoose.connection);
  });
};
于 2021-09-15T09:57:48.717 回答