{"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/3366c21a6311e02f1be91da982f9b93d3cce99a0/src/connection_string.ts#L1184-L1220","documentation":"The `writeConcern` option accepts a `WriteConcern` instance, a plain object, the string `'majority'`, or a number (used as `w`). The transform (src/connection_string.ts:1183-1203) throws 'Invalid WriteConcern cannot parse' for any other type.","triggerScenarios":"`{ writeConcern: 'all' }` (string other than 'majority'); `{ writeConcern: true }`; `{ writeConcern: [1,2] }`; `{ writeConcern: 'majority,local' }`.","commonSituations":"Passing a read-concern-style level like 'local'/'all'; confusing write concern with read concern levels; boolean j flag misuse; array from parsed input.","solutions":["Pass an object: `{ w: 'majority', j: true, wtimeoutMS: 1000 }`","Or `writeConcern: 'majority'`","Or `writeConcern: 1` (numeric w)","Use a WriteConcern instance from `new WriteConcern(...)`"],"exampleFix":"// before\nnew MongoClient(uri, { writeConcern: 'all' });\n// after\nnew MongoClient(uri, { writeConcern: { w: 'majority', j: true, wtimeoutMS: 1000 } });","handlingStrategy":"type-guard","validationCode":"import { WriteConcern } from 'mongodb';\nfunction isWriteConcernInput(v) {\n  return (\n    v instanceof WriteConcern ||\n    (v != null && typeof v === 'object') ||\n    v === 'majority' ||\n    typeof v === 'number'\n  );\n}\nif (options.writeConcern != null && !isWriteConcernInput(options.writeConcern)) {\n  throw new TypeError('writeConcern must be an object, WriteConcern, majority, or number');\n}","typeGuard":"import { WriteConcern } from 'mongodb';\nfunction isWriteConcernLike(v) {\n  return v instanceof WriteConcern || (v != null && typeof v === 'object') || v === 'majority' || typeof v === 'number';\n}","tryCatchPattern":null,"preventionTips":["Pass writeConcern as an object { w, j, wtimeoutMS }","Only the string 'majority' is allowed; other strings are rejected","Use new WriteConcern(...) for explicit construction"],"tags":["write-concern","configuration","options"],"analyzedSha":"3366c21a6311e02f1be91da982f9b93d3cce99a0","analyzedAt":"2026-08-04T13:40:15.335Z","schemaVersion":2}