{"record":{"id":"ae927285473ec515","repo":"mongodb/node-mongodb-native","slug":"topology-cannot-be-constructed-from-json-stringi","errorCode":null,"errorMessage":"Topology cannot be constructed from ${JSON.stringify(seed)}","messagePattern":"Topology cannot be constructed from (.+?)","errorType":"exception","errorClass":"MongoRuntimeError","httpStatus":null,"severity":"error","filePath":"src/sdam/topology.ts","lineNumber":276,"sourceCode":"      hosts: [HostAddress.fromString('localhost:27017')],\n      ...Object.fromEntries(DEFAULT_OPTIONS.entries())\n    };\n\n    if (typeof seeds === 'string') {\n      seeds = [HostAddress.fromString(seeds)];\n    } else if (!Array.isArray(seeds)) {\n      seeds = [seeds];\n    }\n\n    const seedlist: HostAddress[] = [];\n    for (const seed of seeds) {\n      if (typeof seed === 'string') {\n        seedlist.push(HostAddress.fromString(seed));\n      } else if (seed instanceof HostAddress) {\n        seedlist.push(seed);\n      } else {\n        // FIXME(NODE-3483): May need to be a MongoParseError\n        throw new MongoRuntimeError(`Topology cannot be constructed from ${JSON.stringify(seed)}`);\n      }\n    }\n\n    const topologyType = topologyTypeFromOptions(options);\n    const topologyId = globalTopologyCounter++;\n\n    const selectedHosts =\n      options.srvMaxHosts == null ||\n      options.srvMaxHosts === 0 ||\n      options.srvMaxHosts >= seedlist.length\n        ? seedlist\n        : shuffle(seedlist, options.srvMaxHosts);\n\n    const serverDescriptions = new Map();\n    for (const hostAddress of selectedHosts) {\n      serverDescriptions.set(hostAddress.toString(), new ServerDescription(hostAddress));\n    }\n","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/mongodb/node-mongodb-native/blob/dce7939f86fb283e167ad709955abedb7bf23124/src/sdam/topology.ts#L258-L294","documentation":"Thrown by the Topology constructor (src/sdam/topology.ts:276) as MongoRuntimeError when a seed in the seedlist is neither a string nor a HostAddress instance. The constructor accepts a string, HostAddress, or array thereof; any other type (number, object, null) is rejected. Usually indicates a malformed connection string or a programming error passing the wrong object to MongoClient.","triggerScenarios":"Passing a non-string/non-HostAddress seed to MongoClient or Topology, e.g. new MongoClient(42) or internal code that produced an object seed. Connection-string parsing normally yields strings/HostAddresses, so a non-string seed implies an upstream bug or misuse.","commonSituations":"Programmatic construction of a MongoClient with the wrong argument type, a custom URI parser that returns objects, or driver internals mis-shaping seeds.","solutions":["Pass a valid connection string to MongoClient: new MongoClient('mongodb://host:27017').","If constructing a Topology directly (advanced/internal), pass string or HostAddress seeds only.","Log the seed value to identify the source of the bad input."],"exampleFix":"// before\nconst client = new MongoClient({ host: 'localhost' });\n// after\nconst client = new MongoClient('mongodb://localhost:27017');","handlingStrategy":"validation","validationCode":"function assertConnectionString(uri) {\n  if (typeof uri !== 'string') throw new TypeError('connection string must be a string');\n}","typeGuard":"function isConnectionString(v): v is string {\n  return typeof v === 'string' && /^mongodb(\\+srv)?:\\/\\/.+/.test(v);\n}","tryCatchPattern":"try {\n  await client.connect();\n} catch (e) {\n  if (e instanceof MongoRuntimeError && /Topology cannot be constructed/.test(e.message)) {\n    // log the seed value and fix the caller\n  }\n  throw e;\n}","preventionTips":["Always pass a connection string to MongoClient.","Type MongoClient's constructor argument as string | MongoOptions.","Log the connection string at startup to catch malformed seeds."],"tags":["sdam","connection-string","topology","configuration","invalid-argument"],"backgroundTag":null,"analyzedSha":"dce7939f86fb283e167ad709955abedb7bf23124","analyzedAt":"2026-08-11T04:54:53.215Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}