{"record":{"id":"c8ca1f6d17c5c64f","repo":"Automattic/mongoose","slug":"please-provide-an-executor-function","errorCode":null,"errorMessage":"Please provide an executor function","messagePattern":"Please provide an executor function","errorType":"validation","errorClass":"MongooseError","httpStatus":null,"severity":"error","filePath":"lib/connection.js","lineNumber":658,"sourceCode":"\n/**\n * A convenience wrapper for `connection.client.withSession()`.\n *\n * #### Example:\n *\n *     await conn.withSession(async session => {\n *       const doc = await TestModel.findOne().session(session);\n *     });\n *\n * @method withSession\n * @param {Function} executor called with 1 argument: a `ClientSession` instance\n * @return {Promise} resolves to the return value of the executor function\n * @api public\n */\n\nConnection.prototype.withSession = async function withSession(executor) {\n  if (arguments.length === 0) {\n    throw new MongooseError('Please provide an executor function');\n  }\n  return await this.client.withSession(executor);\n};\n\n/**\n * _Requires MongoDB >= 3.6.0._ Starts a [MongoDB session](https://www.mongodb.com/docs/manual/release-notes/3.6/#client-sessions)\n * for benefits like causal consistency, [retryable writes](https://www.mongodb.com/docs/manual/core/retryable-writes/),\n * and [transactions](https://thecodebarbarian.com/a-node-js-perspective-on-mongodb-4-transactions.html).\n *\n * #### Example:\n *\n *     const session = await conn.startSession();\n *     let doc = await Person.findOne({ name: 'Ned Stark' }, null, { session });\n *     await doc.deleteOne();\n *     // `doc` will always be null, even if reading from a replica set\n *     // secondary. Without causal consistency, it is possible to\n *     // get a doc back from the below query if the query reads from a\n *     // secondary that is experiencing replication lag.","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/connection.js#L640-L676","documentation":"Connection.prototype.withSession(executor) borrows a MongoDB ClientSession, passes it to your function, and returns the function's result, handling session cleanup. It throws this error synchronously (surfacing as a rejected promise, since the method is async) when called with zero arguments, because there is no session work to do without an executor. It is a pure API-misuse guard, not a runtime condition.","triggerScenarios":"conn.withSession() with no argument, or conn.withSession(maybeFn) where the variable is undefined (e.g. a refactor removed the body but left the call).","commonSituations":"Skeleton code committed with the executor deleted; conditional executors that are only assigned on some code paths; optional session logic wired through a variable that is undefined in one branch.","solutions":["Pass an async executor: await conn.withSession(async session => { ... })","Guard optional executors before calling: if (typeof fn === 'function') await conn.withSession(fn)"],"exampleFix":"// before\nawait conn.withSession();\n\n// after\nawait conn.withSession(async session => {\n  await Model.findOne().session(session);\n});","handlingStrategy":"validation","validationCode":"if (typeof executor !== 'function') {\n  throw new TypeError('withSession requires an executor function');\n}\nawait conn.withSession(executor);","typeGuard":"const isExecutor = (v) => typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Type the parameter in TypeScript: executor: (session: ClientSession) => Promise<T>","Default optional executors or guard the call site with a typeof check","Treat a zero-argument withSession() as dead code to delete, not handle"],"tags":["session","missing-argument","api-misuse"],"backgroundTag":"missing-required-argument","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}