{"record":{"id":"b1089da6f2882435","repo":"Automattic/mongoose","slug":"connection-prototype-openuri-no-longer-accepts-a","errorCode":null,"errorMessage":"Connection.prototype.openUri() no longer accepts a callback","messagePattern":"Connection\\.prototype\\.openUri\\(\\) no longer accepts a callback","errorType":"exception","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":1162,"sourceCode":" * @api public\n */\n\n// Treat `on('error')` handlers as handling the initialConnection promise\n// to avoid uncaught exceptions when using `on('error')`. See gh-14377.\nConnection.prototype.once = function on(event, callback) {\n  if (event === 'error' && this.$initialConnection) {\n    this.$initialConnection.catch(() => {});\n  }\n  return EventEmitter.prototype.once.call(this, event, callback);\n};\n\n/*!\n * ignore\n */\n\nfunction _validateArgs(uri, options, callback) {\n  if (typeof options === 'function' && callback == null) {\n    throw new MongooseError('Connection.prototype.openUri() no longer accepts a callback');\n  } else if (typeof callback === 'function') {\n    throw new MongooseError('Connection.prototype.openUri() no longer accepts a callback');\n  }\n}\n\n/*!\n * ignore\n */\n\nfunction _handleConnectionErrors(err) {\n  if (err?.name === 'MongoServerSelectionError') {\n    const originalError = err;\n    err = new ServerSelectionError();\n    err.assimilateError(originalError);\n  }\n\n  return err;\n}","sourceCodeStart":1144,"sourceCodeEnd":1180,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L1144-L1180","documentation":"openUri() is the promise-based connection opener behind mongoose.connect() and createConnection(). Mongoose 7 removed callbacks, and _validateArgs (lib/connection.js:1162) throws when the options parameter is a function — the legacy openUri(uri, callback) form. The guard fails fast so the callback is never silently dropped.","triggerScenarios":"conn.openUri('mongodb://...', (err) => { ... }); most commonly mongoose.connect(uri, (err) => { ... }), which forwards to openUri with the function in the options slot (typeof options === 'function' && callback == null).","commonSituations":"Codebases migrated from mongoose 6 or earlier; legacy tutorials and copied snippets; wrapping connect in callback-style helpers or promise-mux libraries.","solutions":["Use promises: await mongoose.connect(uri) or await conn.openUri(uri)","Convert (err) => { ... } bodies into try/catch around the awaited call"],"exampleFix":"// before\nmongoose.connect(uri, (err) => { if (err) return console.error(err); });\n\n// after\ntry {\n  await mongoose.connect(uri);\n} catch (err) {\n  console.error(err);\n}","handlingStrategy":"validation","validationCode":"function openUriSafe(uri, ...rest) {\n  if (rest.some(a => typeof a === 'function')) {\n    throw new TypeError('callback passed to promise-only openUri/connect');\n  }\n  return conn.openUri(uri, ...rest);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always await mongoose.connect(uri, options) — two arguments maximum","Audit bootstrapping code (config/db.js style modules) when upgrading mongoose majors","Register an unhandledRejection handler in development to catch accidentally-unawaited connection promises"],"tags":["connect","openuri","callbacks","migration","mongoose-7"],"backgroundTag":"callback-api-removed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}