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

Getting Javascript error - 'undefined is not an object'

New Here ,
Jan 16, 2022 Jan 16, 2022

I am relatively new to working with scripts in InDesign and have been reading "InDesign CS5 Automation Using XML and Javascript" by Grant Gamble. There's a script I'm trying to execute to update a table in a publication, and I keep getting a Javascript error (Error number: 21 Error String: undefined is not an object) when it executes a function. The error dialog points to the line of code I've bolded below: 

 

function updateTables(){
var doc = app.activeDocument;
for (var i =0; i < doc.pages.length; i++){
var currentPage = doc.pages[i];
var currentFrame;
if(currentPage.textFrames.length > 0 && currentPage.textFrames[0].tables.length > 0){
}
else{
continue;
}
var tableCount = currentFrame.tables.length;
if(tableCount > 0){
var currentTable = currentFrame.tables[0];
var rowCount = currentTable.rows.length;
var currentCuisine = currentFrame.paragraphs[0].contents.replace("\n", "");
for (var j = 0; j < rowCount; j++){
var currentRow = currentTable.rows[j];
var currentCourse = currentRow.cells[0].contents;
var currentKey = currentCuisine + currentCourse;
if(currentKey in g.objDates){
currentRow.contents = g.objDates[currentKey];
}
}
}
}

 

// 

 

I appreciate any guidance on debugging it. Thank you!

 

 

 

 

TOPICS
Bug , Scripting
1.9K
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 2 Correct answers

Community Expert , Jan 16, 2022 Jan 16, 2022

You haven't set the variable currentFrame to anything. var currentPage declares a variable as undefined until you assign a value, eg.

currentFrame = currentPage.textFrames[0];
if (currentFrame.isValid) {
    // do something here
}

You can check the validity of currentPage and currentTable also.

- Mark 

Translate
Engaged , Jan 17, 2022 Jan 17, 2022

Hi @sanjayb75202732 ,

 

Error showing because you did not assign value to the variable.

updateTables();
function updateTables() {
    var doc = app.activeDocument;
    for (var i = 0; i < doc.pages.length; i++) {
        var currentPage = doc.pages[i];
        var currentFrame;
        if (currentPage.textFrames.length > 0 && (currentFrame = currentPage.textFrames[0]).tables.length > 0) {
            var tableCount = currentFrame.tables.length;
            if (tableCount > 0) {
                var 
...
Translate
Community Expert ,
Jan 16, 2022 Jan 16, 2022

You haven't set the variable currentFrame to anything. var currentPage declares a variable as undefined until you assign a value, eg.

currentFrame = currentPage.textFrames[0];
if (currentFrame.isValid) {
    // do something here
}

You can check the validity of currentPage and currentTable also.

- Mark 

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
Community Expert ,
Jan 17, 2022 Jan 17, 2022

To add on to what Mark said, I would say it's good practice to check isValid if there's a chance that object might not exist. For instance if currentFrame doesn't have a table, then currentTable.length would also error. Edit: nevermind I see you are checking currentFrame.tables.length, which also works for the most part.

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
Engaged ,
Jan 17, 2022 Jan 17, 2022

Hi @sanjayb75202732 ,

 

Error showing because you did not assign value to the variable.

updateTables();
function updateTables() {
    var doc = app.activeDocument;
    for (var i = 0; i < doc.pages.length; i++) {
        var currentPage = doc.pages[i];
        var currentFrame;
        if (currentPage.textFrames.length > 0 && (currentFrame = currentPage.textFrames[0]).tables.length > 0) {
            var tableCount = currentFrame.tables.length;
            if (tableCount > 0) {
                var currentTable = currentFrame.tables[0];
                var rowCount = currentTable.rows.length;
                var currentCuisine = currentFrame.paragraphs[0].contents.replace("\n", "");
                for (var j = 0; j < rowCount; j++) {
                    var currentRow = currentTable.rows[j];
                    var currentCourse = currentRow.cells[0].contents;
                    var currentKey = currentCuisine + currentCourse;
                    if (currentKey in g.objDates) {
                        currentRow.contents = g.objDates[currentKey];
                    }
                }
            }
        }
    }
}
-Sumit
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
New Here ,
Jan 24, 2022 Jan 24, 2022
LATEST

Thank you for helping me identify the bug! 

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