{"record":{"id":"6f113727cc425381","repo":"mongodb/node-mongodb-native","slug":"invalid-writeconcern-cannot-parse-json-stringif","errorCode":null,"errorMessage":"Invalid WriteConcern cannot parse: ${JSON.stringify(value)}","messagePattern":"Invalid WriteConcern cannot parse: (.+?)","errorType":"exception","errorClass":"MongoParseError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":1202,"sourceCode":"    target: 'writeConcern',\n    transform({ values: [value], options }) {\n      if (isRecord(value) || value instanceof WriteConcern) {\n        return WriteConcern.fromOptions({\n          writeConcern: {\n            ...options.writeConcern,\n            ...value\n          }\n        });\n      } else if (value === 'majority' || typeof value === 'number') {\n        return WriteConcern.fromOptions({\n          writeConcern: {\n            ...options.writeConcern,\n            w: value\n          }\n        });\n      }\n\n      throw new MongoParseError(`Invalid WriteConcern cannot parse: ${JSON.stringify(value)}`);\n    }\n  },\n  wtimeout: {\n    deprecated: 'Please use wtimeoutMS instead',\n    target: 'writeConcern',\n    transform({ values: [value], options }) {\n      const wc = WriteConcern.fromOptions({\n        writeConcern: {\n          ...options.writeConcern,\n          wtimeout: getUIntFromOptions('wtimeout', value)\n        }\n      });\n      if (wc) return wc;\n      throw new MongoParseError(`Cannot make WriteConcern from wtimeout`);\n    }\n  } as OptionDescriptor,\n  wtimeoutMS: {\n    target: 'writeConcern',","sourceCodeStart":1184,"sourceCodeEnd":1220,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L1184-L1220","documentation":"The `writeConcern` option transform at src/connection_string.ts:1185 accepts four shapes: an object/WriteConcern instance (record check), the literal string `'majority'`, or a number (used as `w`). Any other value falls through to MongoParseError at src/connection_string.ts:1202.","triggerScenarios":"Passing `writeConcern: 'all'`, `writeConcern: true`, `writeConcern: ['majority']`, `writeConcern: '1'` (string number, not number), or `writeConcern: null`.","commonSituations":"Confusing with `w: 'majority'` syntax; passing a JSON-decoded value that became a string; treating writeConcern as a boolean 'enable safe writes' flag.","solutions":["Pass an object: `writeConcern: { w: 'majority', j: true }`.","Or a numeric w: `writeConcern: 1` (number, not string).","Or the literal string: `writeConcern: 'majority'`."],"exampleFix":"// before\nnew MongoClient(uri, { writeConcern: 'all' });\n\n// after\nnew MongoClient(uri, { writeConcern: { w: 'majority' } });","handlingStrategy":"type-guard","validationCode":"import { WriteConcern } from 'mongodb';\nfunction isWriteConcernInput(v: unknown): boolean {\n  return v instanceof WriteConcern || (typeof v === 'object' && v !== null) || v === 'majority' || typeof v === 'number';\n}\nif (opts.writeConcern != null && !isWriteConcernInput(opts.writeConcern)) {\n  throw new Error('writeConcern must be an object, WriteConcern, \"majority\", or a number');\n}","typeGuard":"function isWriteConcernInput(v: unknown): v is object | 'majority' | number { return (typeof v === 'object' && v !== null) || v === 'majority' || typeof v === 'number'; }","tryCatchPattern":"try { client = new MongoClient(uri, opts); } catch (e) { if (e instanceof MongoParseError && /WriteConcern cannot parse/.test(e.message)) { opts.writeConcern = { w: 'majority' }; client = new MongoClient(uri, opts); } else throw e; }","preventionTips":["Always pass writeConcern as an object `{ w, j, wtimeoutMS }`.","Use numeric `w` values as numbers, not strings.","Type-check values decoded from JSON before forwarding."],"tags":["write-concern","configuration","type-error"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}