【问题标题】:No AuthProvider for DEFAULT defined with mongoose 6没有使用 mongoose 6 定义的 DEFAULT 的 AuthProvider
【发布时间】:2021-09-15 09:57:48
【问题描述】:

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

【问题讨论】:

    标签: node.js mongodb mongoose mongodb-nodejs-driver


    【解决方案1】:

    对于无密码/无用户连接,确保连接 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-10-02
      • 2022-01-09
      • 2017-06-25
      • 2016-10-02
      • 1970-01-01
      • 2020-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多