{"record":{"id":"01c8893a27ae7ffb","repo":"trekhleb/javascript-algorithms","slug":"graph-vertex-must-have-a-value","errorCode":null,"errorMessage":"Graph vertex must have a value","messagePattern":"Graph vertex must have a value","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/data-structures/graph/GraphVertex.js","lineNumber":9,"sourceCode":"import LinkedList from '../linked-list/LinkedList';\n\nexport default class GraphVertex {\n  /**\n   * @param {*} value\n   */\n  constructor(value) {\n    if (value === undefined) {\n      throw new Error('Graph vertex must have a value');\n    }\n\n    /**\n     * @param {GraphEdge} edgeA\n     * @param {GraphEdge} edgeB\n     */\n    const edgeComparator = (edgeA, edgeB) => {\n      if (edgeA.getKey() === edgeB.getKey()) {\n        return 0;\n      }\n\n      return edgeA.getKey() < edgeB.getKey() ? -1 : 1;\n    };\n\n    // Normally you would store string value like vertex name.\n    // But generally it may be any object as well\n    this.value = value;\n    this.edges = new LinkedList(edgeComparator);","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/trekhleb/javascript-algorithms/blob/85293e3e2b88f4d2ce330d956b139cf628aa1e82/src/data-structures/graph/GraphVertex.js#L1-L27","documentation":"GraphVertex's constructor requires a value (src/data-structures/graph/GraphVertex.js:8-10) because the value doubles as the vertex key - getKey() returns this.value verbatim - and every graph lookup depends on that key existing. Only undefined is rejected: null, 0, '' and false are all accepted as legitimate values.","triggerScenarios":"new GraphVertex() with no argument; new GraphVertex(obj.missingProperty) where parsing or destructuring produced undefined; new GraphVertex(row.name) where some records lack the field; a helper with an optional parameter that forwards undefined into the constructor.","commonSituations":"Building vertices from JSON/CSV rows where a column is missing or null-vs-absent varies between records; refactors that rename the field feeding the constructor; optional function parameters that silently propagate undefined.","solutions":["Supply a concrete value or default: new GraphVertex(row.label ?? `vertex-${row.id}`) - only undefined throws, so null and falsy primitives are fine when intentional.","Validate records at the boundary and fail with your own clearer error before reaching the constructor.","If 'no value' is meaningful in your domain, pass null explicitly rather than leaving the argument out."],"exampleFix":"// before\nconst vertex = new GraphVertex(row.label); // row.label === undefined -> Error\n\n// after\nconst vertex = new GraphVertex(row.label ?? `vertex-${row.id}`);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// undefined is the ONLY rejected value - null, 0, '' and false are valid\nconst hasVertexValue = (value) => value !== undefined;\n\nif (hasVertexValue(row.label)) {\n  graph.addVertex(new GraphVertex(row.label));\n} else {\n  // record is malformed: report it before constructing\n}","tryCatchPattern":"try {\n  vertex = new GraphVertex(candidate);\n} catch (error) {\n  if (error.message === 'Graph vertex must have a value') {\n    vertex = new GraphVertex(fallbackValue);\n  } else {\n    throw error;\n  }\n}","preventionTips":["Give the constructor call a fallback (?? default) whenever the input is optional.","Validate parsed rows for required fields before graph construction.","Remember null is legal - do not add truthiness checks that reject 0 or ''."],"tags":["graph","vertex","constructor","undefined-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"85293e3e2b88f4d2ce330d956b139cf628aa1e82","analyzedAt":"2026-08-24T05:59:10.417Z","schemaVersion":2},"datasetVersion":"2026-08-24T07:17:09.176Z"}