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

Why am I getting a TypeError?

Participant ,
Jan 22, 2020 Jan 22, 2020

Copy link to clipboard

Copied

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

Views

342

Translate

Translate

Report

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.

Votes

Translate

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

Copy link to clipboard

Copied

What is the code of the function getFieldOn?

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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;
    }
}

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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