Why am I getting a TypeError?

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

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.

TOPICS
Acrobat SDK and JavaScript, General troubleshooting, PDF forms

Views

60

Likes

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

Why am I getting a TypeError?

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

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.

TOPICS
Acrobat SDK and JavaScript, General troubleshooting, PDF forms

Views

61

Likes

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
Jan 22, 2020 0
Jan 22, 2020

Copy link to clipboard

Copied

What is the code of the function getFieldOn?

Likes

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
Reply
Loading...
Jan 22, 2020 0
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;
    }
}

Likes

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
Reply
Loading...
Jan 22, 2020 0
Jan 22, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jan 22, 2020 0
Jan 22, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Jan 22, 2020 0
Jan 22, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Jan 22, 2020 0