mongodb/node-mongodb-native · error · MongoAPIError

User input for option 'mongodbLogComponentSeverities' object

Error message

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

What it means

Thrown while iterating entries of mongodbLogComponentSeverities when a key or value is not a string. Both keys (component names) and values (severity levels) must be string-typed; non-string keys can appear via numeric object keys, and non-string values via booleans/numbers.

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 3366c21a63)

Solutions

  1. Quote all severity values: { command: 'debug' } not { command: 1 }.
  2. Ensure config loaders (YAML/ENV parsers) emit strings — quote values or cast with String().
  3. Validate the object shape before constructing MongoClient.

Example fix

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

Strategy: validation

Validate before calling

function validateSeverities(o) {
  for (const [k, v] of Object.entries(o)) {
    if (typeof k !== 'string' || typeof v !== 'string') return false;
  }
  return true;
}

Type guard

function isStringRecord(o): o is Record<string, string> {
  return Object.values(o).every(v => typeof v === 'string');
}

Prevention

When it happens

Trigger: Passing { command: 1 } (numeric severity), { 0: 'debug' } (numeric key auto-coerced), { command: true }, or any entry where Object.entries yields a non-string side. Symbol-keyed entries are excluded by Object.entries but numeric literals become string keys so the value side is the usual culprit.

Common situations: Porting a config where severities were numbers (e.g. syslog-style 0-7); loading config from YAML that parsed severity as an int; copy-pasting example that used unquoted numbers.

Related errors


AI-assisted analysis of mongodb/node-mongodb-native@3366c21a63 (2026-08-04). Data as JSON: /data/errors/fb5885b6e25c23b4.json. Report an issue: GitHub.