mongodb/node-mongodb-native · error · MongoAPIError

User input for option 'mongodbLogComponentSeverities'…

Error message

User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value

What it means

After confirming the value is a non-null object, the transform at src/connection_string.ts:1259 iterates entries and requires BOTH the key and the value to be strings. If any value is non-string (e.g. a numeric severity) or any key is non-string (e.g. a symbol/number), MongoAPIError is thrown at src/connection_string.ts:1261.

Solutions

  1. Use string severity names for every value: `{ command: 'error' }`.
  2. Quote numeric-looking keys or keep keys as component names: `{ command: 'debug' }`.
  3. Validate every entry is `[string, string]` before constructing the client.

Example fix

// before
new MongoClient(uri, { mongodbLogComponentSeverities: { command: 3 } });

// after
new MongoClient(uri, { mongodbLogComponentSeverities: { command: 'error' } });
Defensive patterns

Strategy: validation

Validate before calling

if (opts.mongodbLogComponentSeverities) {
  for (const [k, v] of Object.entries(opts.mongodbLogComponentSeverities as Record<string, unknown>)) {
    if (typeof k !== 'string' || typeof v !== 'string') {
      throw new Error(`component severity entry ${k} must be [string, string]`);
    }
  }
}

Type guard

function isStringStringRecord(v: unknown): v is Record<string, string> { return typeof v === 'object' && v !== null && Object.entries(v).every(([k, val]) => typeof k === 'string' && typeof val === 'string'); }

Try / catch

try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoAPIError && /non-string key or value/.test(e.message)) { opts.mongodbLogComponentSeverities = { default: 'info' }; client = new MongoClient(uri, opts); } else throw e; }

Prevention

When it happens

Trigger: Passing `mongodbLogComponentSeverities: { command: 3 }` (numeric severity from a syslog table); `{ 0: 'debug' }` (numeric key); `{ command: true }` (boolean toggle).

Common situations: Copying numeric syslog severities from documentation; using config formats that auto-coerce keys to numbers; treating severities as on/off booleans.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@dce7939f86 (2026-08-11). Data as JSON: /api/errors/fb5885b6e25c23b4. Report an issue: GitHub.

Appendix: source

Thrown at src/connection_string.ts:1261

            'write' in value &&
            typeof value.write === 'function')
        )
      ) {
        throw new MongoAPIError(
          `Option 'mongodbLogPath' must be of type 'stderr' | 'stdout' | MongoDBLogWritable`
        );
      }
      return value;
    }
  },
  mongodbLogComponentSeverities: {
    transform({ values: [value] }) {
      if (typeof value !== 'object' || !value) {
        throw new MongoAPIError(`Option 'mongodbLogComponentSeverities' must be a non-null object`);
      }
      for (const [k, v] of Object.entries(value)) {
        if (typeof v !== 'string' || typeof k !== 'string') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' object cannot include a non-string key or value`
          );
        }
        if (!Object.values(MongoLoggableComponent).some(val => val === k) && k !== 'default') {
          throw new MongoAPIError(
            `User input for option 'mongodbLogComponentSeverities' contains invalid key: ${k}`
          );
        }
        if (!Object.values(SeverityLevel).some(val => val === v)) {
          throw new MongoAPIError(
            `Option 'mongodbLogComponentSeverities' does not support ${v} as a value for ${k}`
          );
        }
      }
      return value;
    }
  },
  mongodbLogMaxDocumentLength: { type: 'uint' },

View on GitHub (pinned to dce7939f86)