{"record":{"id":"1b2328434a5fe4a9","repo":"mongodb/node-mongodb-native","slug":"uri-option-key-cannot-be-specified-with-no-va","errorCode":null,"errorMessage":"URI option \"${key}\" cannot be specified with no value","messagePattern":"URI option \"(.+?)\" cannot be specified with no value","errorType":"exception","errorClass":"MongoAPIError","httpStatus":null,"severity":"error","filePath":"src/connection_string.ts","lineNumber":298,"sourceCode":"      auth.password = decodeURIComponent(url.password);\n    }\n\n    urlOptions.set('auth', [auth]);\n  }\n\n  for (const key of url.searchParams.keys()) {\n    const values = url.searchParams.getAll(key);\n\n    const isReadPreferenceTags = /readPreferenceTags/i.test(key);\n\n    if (!isReadPreferenceTags && values.length > 1) {\n      throw new MongoInvalidArgumentError(\n        `URI option \"${key}\" cannot appear more than once in the connection string`\n      );\n    }\n\n    if (!isReadPreferenceTags && values.includes('')) {\n      throw new MongoAPIError(`URI option \"${key}\" cannot be specified with no value`);\n    }\n\n    if (!urlOptions.has(key)) {\n      urlOptions.set(key, values);\n    }\n  }\n\n  const objectOptions = new CaseInsensitiveMap<unknown>(\n    Object.entries(options).filter(([, v]) => v != null)\n  );\n\n  // Validate options that can only be provided by one of uri or object\n\n  if (urlOptions.has('serverApi')) {\n    throw new MongoParseError(\n      'URI cannot contain `serverApi`, it can only be passed to the client'\n    );\n  }","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/connection_string.ts#L280-L316","documentation":"Every URI query parameter (except readPreferenceTags, which may legitimately be valueless) must have a value. A key with an empty value, or a '?&key&key2=v' style fragment, yields an empty string in the search params and triggers MongoAPIError.","triggerScenarios":"A URI like 'mongodb://h/db?tls' or 'mongodb://h/db?appName=&retryWrites=true'. Triggered while iterating url.searchParams.keys() when values.includes('').","commonSituations":"Conditional query-string assembly that leaves a key with no value, trailing '&', or a malformed URI from an env var.","solutions":["Provide an explicit value for every query parameter.","Strip keys that should not be set instead of leaving them empty."],"exampleFix":"// before\n'mongodb://h/db?tls&appName=foo'\n// after\n'mongodb://h/db?tls=true&appName=foo'","handlingStrategy":"validation","validationCode":"function assertNoEmptyValues(uri: string) {\n  const q = uri.split('?')[1];\n  if (!q) return;\n  for (const pair of q.split('&')) {\n    if (!pair) continue;\n    const [key, ...rest] = pair.split('=');\n    if (key === 'readPreferenceTags') continue;\n    if (rest.length === 0) throw new Error(`URI option '${key}' has no value`);\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Generate the query string with URLSearchParams so empty values surface.","Avoid templating that may leave empty fragments."],"tags":["connection-string","validation","uri"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}