{"record":{"id":"113eaa3468c30e3c","repo":"Automattic/mongoose","slug":"cannot-call-setclient-with-a-mongoclient-that","errorCode":null,"errorMessage":"Cannot call `setClient()` with a MongoClient that you have not called `connect()` on yet.","messagePattern":"Cannot call `setClient\\(\\)` with a MongoClient that you have not called `connect\\(\\)` on yet\\.","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/drivers/node-mongodb-native/connection.js","lineNumber":418,"sourceCode":"\n  return {\n    schemaMap, encryptedFieldsMap\n  };\n};\n\n/*!\n * ignore\n */\n\nNativeConnection.prototype.setClient = function setClient(client) {\n  if (!(client instanceof mongodb.MongoClient)) {\n    throw new MongooseError('Must call `setClient()` with an instance of MongoClient');\n  }\n  if (this.readyState !== STATES.disconnected) {\n    throw new MongooseError('Cannot call `setClient()` on a connection that is already connected.');\n  }\n  if (client.topology == null) {\n    throw new MongooseError('Cannot call `setClient()` with a MongoClient that you have not called `connect()` on yet.');\n  }\n\n  this._connectionString = client.s.url;\n  _setClient(this, client, {}, client.s.options.dbName);\n\n  for (const model of Object.values(this.models)) {\n    // Errors handled internally, so safe to ignore error\n    model.init().catch(function $modelInitNoop() {});\n  }\n\n  return this;\n};\n\n/*!\n * ignore\n */\n\nfunction _setClient(conn, client, options, dbName) {","sourceCodeStart":400,"sourceCodeEnd":436,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/drivers/node-mongodb-native/connection.js#L400-L436","documentation":"setClient() requires an already-connected MongoClient: it checks client.topology != null, which is only set after await client.connect() succeeds. Passing a freshly constructed, unconnected client throws this MongooseError because Mongoose would otherwise serve queries against a client with no server topology.","triggerScenarios":"const client = new MongoClient(uri); conn.setClient(client); without await client.connect(); - also when connect() was started but not awaited, leaving topology null at call time.","commonSituations":"Copy-pasting driver examples that stop at new MongoClient(uri); assuming Mongoose performs the connect for you; a forgotten await on client.connect().","solutions":["Connect the client first: await client.connect(); then await conn.setClient(client).","Or skip manual client management: await conn.openUri(uri) / mongoose.connect(uri) handles connect internally."],"exampleFix":"// before\nconst client = new MongoClient(uri);\nconn.setClient(client); // throws: topology is null\n\n// after\nconst client = new MongoClient(uri);\nawait client.connect();\nawait conn.setClient(client);","handlingStrategy":"validation","validationCode":"if (client.topology == null) {\n  await client.connect();\n}\nawait conn.setClient(client);","typeGuard":"function isClientConnected(client) {\n  return client.topology != null;\n}","tryCatchPattern":null,"preventionTips":["Always await client.connect() before handing a client to Mongoose.","Prefer mongoose.connect()/openUri() unless you specifically share the client with other code.","Chain it safely: await conn.setClient(await new MongoClient(uri).connect());"],"tags":["mongoose","setclient","mongodb-driver","connect","await"],"backgroundTag":"database-not-connected","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}