mongodb/node-mongodb-native · error · MongoInvalidArgumentError
Missing required callback parameter
Error message
Missing required callback parameter
What it means
Thrown by MongoClient.withSession() when no executor callback is provided. withSession has overloads accepting (executor) or (options, executor); if neither resolves to a function, withSessionCallback is null/undefined and the call is rejected as MongoInvalidArgumentError. The callback receives the session and is where your session-scoped work runs.
Source
Thrown at src/mongo_client.ts:939
options: ClientSessionOptions,
executor: WithSessionCallback<T>
): Promise<T>;
async withSession<T = any>(
optionsOrExecutor: ClientSessionOptions | WithSessionCallback<T>,
executor?: WithSessionCallback<T>
): Promise<T> {
const options = {
// Always define an owner
owner: Symbol(),
// If it's an object inherit the options
...(typeof optionsOrExecutor === 'object' ? optionsOrExecutor : {})
};
const withSessionCallback =
typeof optionsOrExecutor === 'function' ? optionsOrExecutor : executor;
if (withSessionCallback == null) {
throw new MongoInvalidArgumentError('Missing required callback parameter');
}
const session = this.startSession(options);
try {
return await withSessionCallback(session);
} finally {
try {
await session.endSession();
} catch (error) {
squashError(error);
}
}
}
/**
* Create a new Change Stream, watching for new changes (insertions, updates,
* replacements, deletions, and invalidations) in this cluster. Will ignore allView on GitHub (pinned to 3366c21a63)
Solutions
- Always pass a callback: await client.withSession(session => { ... });
- Use the two-arg form only when you need options: await client.withSession({ causalConsistency: true }, session => { ... });
- If you only need a session handle, use const session = client.startSession() and remember session.endSession().
Example fix
// before
await client.withSession(); // throws
// after
await client.withSession(async session => {
await collection.insertOne(doc, { session });
}); Defensive patterns
Strategy: validation
Validate before calling
function assertWithSessionArgs(optionsOrExecutor, executor) {
const cb = typeof optionsOrExecutor === 'function' ? optionsOrExecutor : executor;
if (typeof cb !== 'function') throw new TypeError('withSession requires a callback');
} Type guard
function isFn(v): v is Function { return typeof v === 'function'; } Prevention
- Always pass a callback to withSession
- Prefer startSession/endSession for explicit control
- Type-check options vs callback at call sites
When it happens
Trigger: Calling client.withSession() with no arguments; calling client.withSession({}) with only options and no executor; passing the callback as a non-function (e.g. an options object in both slots).
Common situations: Refactoring from callback-style to async code and forgetting the callback; copy-paste errors; calling withSession expecting it to just create and return a session.
Related errors
- ChangeStream cannot be used as an EventEmitter after being u
- ChangeStream cannot be used as an iterator after being used
- A change stream document has been received that lacks a resu
- User provided OIDC callbacks must return a valid object with
- OIDC callback timed out after ${HUMAN_TIMEOUT_MS}ms.
AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04).
Data as JSON: /data/errors/e084906f3da5af6f.json.
Report an issue: GitHub.