{"record":{"id":"8d4597b2b4bbe4e2","repo":"mozilla/pdf.js","slug":"invalid-field-index-must-be-a-number","errorCode":null,"errorMessage":"Invalid field index: must be a number","messagePattern":"Invalid field index: must be a number","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/scripting_api/doc.js","lineNumber":1025,"sourceCode":"\n  getIcon() {\n    /* Not implemented */\n  }\n\n  getLegalWarnings() {\n    /* Not implemented */\n  }\n\n  getLinks() {\n    /* Not implemented */\n  }\n\n  getNthFieldName(nIndex) {\n    if (nIndex && typeof nIndex === \"object\") {\n      nIndex = nIndex.nIndex;\n    }\n    if (typeof nIndex !== \"number\") {\n      throw new TypeError(\"Invalid field index: must be a number\");\n    }\n    if (0 <= nIndex && nIndex < this.numFields) {\n      return this._fieldNames[Math.trunc(nIndex)];\n    }\n    return null;\n  }\n\n  getNthTemplate() {\n    return null;\n  }\n\n  getOCGs() {\n    /* Not implemented */\n  }\n\n  getOCGOrder() {\n    /* Not implemented */\n  }","sourceCodeStart":1007,"sourceCodeEnd":1043,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/doc.js#L1007-L1043","documentation":"Thrown by `Doc.getNthFieldName(nIndex)` (src/scripting_api/doc.js:1025). After unwrapping an optional `{nIndex}` object, it requires a numeric index. Returns the field name at that position or `null` if out of range — only the type mismatch throws, not an out-of-bounds index.","triggerScenarios":"Calling `doc.getNthFieldName('0')` (string index), `doc.getNthFieldName()` (undefined), or `doc.getNthFieldName(fieldRef)` passing a field object instead of an integer. Iterating with a string counter from a parsed form value.","commonSituations":"Loops where the counter is read from a form field value (always a string in the Acrobat API) and not coerced; passing an object whose key is not `nIndex`; UI code that threads a 1-based display index straight through.","solutions":["Coerce to a number: `doc.getNthFieldName(Number(i))`.","Validate the index is a finite number and within `[0, doc.numFields)` before calling.","When passing an object, use the key `nIndex` (e.g. `{nIndex: 0}`)."],"exampleFix":"// before\nfor (let i = field.value; i < doc.numFields; i++) {\n  console.println(doc.getNthFieldName(i)); // field.value is a string\n}\n// after\nfor (let i = Number(field.value); i < doc.numFields; i++) {\n  console.println(doc.getNthFieldName(i));\n}","handlingStrategy":"type-guard","validationCode":"function getNthFieldNameSafe(doc, nIndex) {\n  if (nIndex && typeof nIndex === 'object') nIndex = nIndex.nIndex;\n  if (typeof nIndex !== 'number' || !Number.isFinite(nIndex)) return null;\n  return doc.getNthFieldName(nIndex);\n}","typeGuard":"function isFieldIndex(v) {\n  if (v && typeof v === 'object') v = v.nIndex;\n  return typeof v === 'number' && Number.isFinite(v);\n}","tryCatchPattern":null,"preventionTips":["Coerce loop counters with Number() when sourced from field values (always strings).","Bound-check against doc.numFields before iterating.","Use the nIndex key when passing an object argument."],"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"}