{"record":{"id":"d582eecce5e3bb73","repo":"mozilla/pdf.js","slug":"invalid-field-name-must-be-a-string","errorCode":null,"errorMessage":"Invalid field name: must be a string","messagePattern":"Invalid field name: must be a string","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":931,"sourceCode":"\n  getColorConvertAction() {\n    /* Not implemented */\n  }\n\n  getDataObject() {\n    /* Not implemented */\n  }\n\n  getDataObjectContents() {\n    /* Not implemented */\n  }\n\n  _getField(cName) {\n    if (cName && typeof cName === \"object\") {\n      cName = cName.cName;\n    }\n    if (typeof cName !== \"string\") {\n      throw new TypeError(\"Invalid field name: must be a string\");\n    }\n    const searchedField = this._fields.get(cName);\n    if (searchedField) {\n      return searchedField;\n    }\n\n    const parts = cName.split(\"#\");\n    let childIndex = NaN;\n    if (parts.length === 2) {\n      childIndex = Math.floor(parseFloat(parts[1]));\n      cName = parts[0];\n    }\n\n    for (const [name, field] of this._fields) {\n      if (name.endsWith(cName)) {\n        if (!isNaN(childIndex)) {\n          const children = this._getChildren(name);\n          if (childIndex < 0 || childIndex >= children.length) {","sourceCodeStart":913,"sourceCodeEnd":949,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L913-L949","documentation":"Thrown inside `Doc._getField(cName)` (src/scripting_api/doc.js:931), the helper behind `doc.getField()` and several field-lookup APIs. After unwrapping an optional `{cName}` object, it asserts the remaining value is a string. It is a `TypeError`, reflecting that the field-name parameter has the wrong type, not just a missing field.","triggerScenarios":"Calling `doc.getField()` with no argument, or with a number/boolean/Array, e.g. `doc.getField(3)`, `doc.getField()`, `doc.getField(someNumber)`. The object-unwrap (`cName = cName.cName`) only fires when the argument is an object, so passing `{nIndex: 0}` (wrong key) falls through and throws.","commonSituations":"Dynamic field lookups where the name comes from a variable that is sometimes a number (an index) or undefined; destructured parameter objects whose key does not match `cName`; calling `getField` from a loop counter without `.toString()`.","solutions":["Coerce the lookup key to a string before calling: `doc.getField(String(fieldName))`.","Guard against undefined/null and return early when the name is absent.","When passing an object, ensure the property key is exactly `cName` (e.g. `{cName: 'foo'}`)."],"exampleFix":"// before\nconst f = doc.getField(this.getFieldAt(i)); // index passed by mistake\n// after\nconst name = doc.getNthFieldName(i);\nconst f = name ? doc.getField(String(name)) : null;","handlingStrategy":"type-guard","validationCode":"function getFieldSafe(doc, cName) {\n  if (cName && typeof cName === 'object') cName = cName.cName;\n  if (typeof cName !== 'string' || cName.length === 0) return null;\n  return doc.getField(cName);\n}","typeGuard":"function isFieldName(v) {\n  if (v && typeof v === 'object') v = v.cName;\n  return typeof v === 'string' && v.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Always coerce field names with String(...) before lookup.","When passing an object, use the exact key cName ({cName: 'foo'}).","Guard empty/undefined names and return null instead of relying on the throw."],"tags":["scripting-api","type-check","field-lookup","acrobat-js"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}