Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Why am I getting a TypeError?

Participant ,
Jan 22, 2020 Jan 22, 2020

I have a document level script for updating my page numbers:

function updatePg(){
    var f;
    for(var p = 0; p < this.numPages; p++){
        f = getFieldOn("pgN", p);
        f.value = "Page  " + (p + 1) + " of ";
        this.getField("pgT").value = this.numPages;
    }
}

I then have it called from a button that spawns pages. It was working, but later without changing that code, I started getting 

    TypeError: f is undefined
    6:Document-Level:updatePg

TOPICS
Acrobat SDK and JavaScript
738
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Participant , Jan 22, 2020 Jan 22, 2020

I figured it out. I don't have the pgN field on my template, and when it's unhidden for me to edit it, the function doesn't work. I need to add an if statement to see if the field exists before trying to change it.

Translate
Community Expert ,
Jan 22, 2020 Jan 22, 2020

What is the code of the function getFieldOn?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2020 Jan 22, 2020
function getFieldOn(name, p){//finds (the first) field on page p with 'name' in its name
    var fields;
    for(var i = 0; i < this.numFields; i++){
        var f = this.getField(this.getNthFieldName(i));
        if ((f.name.indexOf(name) > -1) && (f.page == p)) return f;
    }
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2020 Jan 22, 2020

getFieldOn is also a document level function. Is this an issue?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2020 Jan 22, 2020

Is using 'f' as a variable in both places a problem?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jan 22, 2020 Jan 22, 2020
LATEST

I figured it out. I don't have the pgN field on my template, and when it's unhidden for me to edit it, the function doesn't work. I need to add an if statement to see if the field exists before trying to change it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines