mozilla/pdf.js · error · TypeError
Invalid field name: must be a string
Error message
Invalid field name: must be a string
What it means
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.
Source
Thrown at src/scripting_api/doc.js:931
getColorConvertAction() {
/* Not implemented */
}
getDataObject() {
/* Not implemented */
}
getDataObjectContents() {
/* Not implemented */
}
_getField(cName) {
if (cName && typeof cName === "object") {
cName = cName.cName;
}
if (typeof cName !== "string") {
throw new TypeError("Invalid field name: must be a string");
}
const searchedField = this._fields.get(cName);
if (searchedField) {
return searchedField;
}
const parts = cName.split("#");
let childIndex = NaN;
if (parts.length === 2) {
childIndex = Math.floor(parseFloat(parts[1]));
cName = parts[0];
}
for (const [name, field] of this._fields) {
if (name.endsWith(cName)) {
if (!isNaN(childIndex)) {
const children = this._getChildren(name);
if (childIndex < 0 || childIndex >= children.length) {View on GitHub (pinned to 5903d58d58)
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'}`).
Example fix
// before const f = doc.getField(this.getFieldAt(i)); // index passed by mistake // after const name = doc.getNthFieldName(i); const f = name ? doc.getField(String(name)) : null;
Defensive patterns
Strategy: type-guard
Validate before calling
function getFieldSafe(doc, cName) {
if (cName && typeof cName === 'object') cName = cName.cName;
if (typeof cName !== 'string' || cName.length === 0) return null;
return doc.getField(cName);
} Type guard
function isFieldName(v) {
if (v && typeof v === 'object') v = v.cName;
return typeof v === 'string' && v.length > 0;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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()`.
Related errors
- Invalid field index: must be a number
- Invalid argument value
- First argument of printf must be a string
- app.activeDocs is read-only
- app.constants is read-only
AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13).
Data as JSON: /api/errors/d582eecce5e3bb73.
Report an issue: GitHub.