{"id":"ba1825c4cd65b44d","repo":"mongodb/node-mongodb-native","slug":"name-must-be-an-object","errorCode":null,"errorMessage":"${name} must be an object","messagePattern":"(.+?) must be an object","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":617,"sourceCode":"      break;\n    case 'int':\n      mongoOptions[name] = getIntFromOptions(name, values[0]);\n      break;\n    case 'uint':\n      mongoOptions[name] = getUIntFromOptions(name, values[0]);\n      break;\n    case 'string':\n      if (values[0] == null) {\n        break;\n      }\n      // The value should always be a string here, but since the array is typed as unknown\n      // there still needs to be an explicit cast.\n      // eslint-disable-next-line @typescript-eslint/no-base-to-string\n      mongoOptions[name] = String(values[0]);\n      break;\n    case 'record':\n      if (!isRecord(values[0])) {\n        throw new MongoParseError(`${name} must be an object`);\n      }\n      mongoOptions[name] = values[0];\n      break;\n    case 'any':\n      mongoOptions[name] = values[0];\n      break;\n    default: {\n      if (!transform) {\n        throw new MongoParseError('Descriptors missing a type must define a transform');\n      }\n      const transformValue = transform({ name, options: mongoOptions, values });\n      mongoOptions[name] = transformValue;\n      break;\n    }\n  }\n}\n\ninterface OptionDescriptor {","sourceCodeStart":599,"sourceCodeEnd":635,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L599-L635","documentation":"Thrown by setOption (connection_string.ts:616-618) for options whose descriptor type is 'record' (e.g. autoEncryption, driverInfo, fieldsAsRaw) when the provided value is not a plain object. The isRecord helper rejects arrays, primitives, null, and non-plain objects because record options are always key-value maps.","triggerScenarios":"Options { autoEncryption: 'enabled' } (string instead of object), { driverInfo: ['x'] } (array), or passing a JSON string instead of a parsed object.","commonSituations":"Reading config from a string source (env var, file) and forgetting to JSON.parse it; passing a boolean flag where a configuration object is expected; using an array for fieldsAsRaw.","solutions":["Pass a plain object literal (e.g. { autoEncryption: { keyVaultNamespace: 'enc.kv', kmsProviders: {...} } }).","If the value comes from a string source, JSON.parse it before passing.","For fieldsAsRaw / driverInfo, ensure the value is { } with string keys."],"exampleFix":"// before\nconst c = new MongoClient(uri, { autoEncryption: process.env.AUTO_ENC }); // string!\n// after\nconst c = new MongoClient(uri, { autoEncryption: JSON.parse(process.env.AUTO_ENC) });","handlingStrategy":"type-guard","validationCode":"for (const k of ['autoEncryption', 'driverInfo', 'fieldsAsRaw']) {\n  if (k in opts && (typeof opts[k] !== 'object' || opts[k] === null || Array.isArray(opts[k]))) {\n    throw new Error(`Option ${k} must be a plain object`);\n  }\n}","typeGuard":"import type { MongoClientOptions } from 'mongodb';\nfunction isPlainObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null && !Array.isArray(v) && Object.getPrototypeOf(v) === Object.prototype;\n}\nfunction assertRecordOptions(opts: Partial<MongoClientOptions>) {\n  for (const k of ['autoEncryption', 'driverInfo', 'fieldsAsRaw'] as const) {\n    if (k in opts && !isPlainObject(opts[k])) throw new Error(`${k} must be an object`);\n  }\n}","tryCatchPattern":null,"preventionTips":["JSON.parse string-sourced config before assigning to record options.","Type config strictly as MongoClientOptions so non-object values fail at compile time.","Avoid passing arrays where a key-value map is expected."],"tags":["connection-string","configuration","csfle","typing"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}