problem with pushing object into array

davilu
Mega Sage

Hi Everyone, I have a GlideRecord query that pushes certain objects into an array:  

var wf = new GlideRecord(wf_stage_table);

wf.addQuery('workflow_version.workflow', bundle.workflow);

wf.addQuery('workflow_version.published', true);

wf.orderBy('order');

wf.query();

while (wf.next()) {

      data.stage.push({

              workflow_stage: wf.getDisplayValue('name'),

              task: [],

              incompleteTotal: 0

      });

}

At the end of all my code, I wanted to total up incomplete tasks and then push it back into my data.stage array.  

var total = 0;

for (var i = 0; i < data.stage.length; i++) {

      var stage = data.stage[i].task;

      for (var j = 0; j < stage.length; j++) {

              total += stage[j].incompleteCounter

      }

}

data.stage.incompleteTotal.push = total;

The above code obviously doesn't work...I get an error that says Server JavaScript error Cannot set property "push" of undefined to "2"

What is wrong with my syntax above?   The total is correct, I just need to push it back into my array.

1 ACCEPTED SOLUTION

Thanks sanjivmeher!



I followed your code, but changed:   data.stage[i].incompleteTotal = stage[j].incompleteCounter; to data.stage[i].incompleteTotal = total and it works!   Thanks!





View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

I think the script shoul be as below. Because incompleteCounter is again an array under data.stage.


If you want that to be an individual paramter, you should declare it separately instead of inside the while loop. something like data.stage.incompleteTotal = 0;



var total = 0;


for (var i = 0; i < data.stage.length; i++) {


      var stage = data.stage[i].task;


      for (var j = 0; j < stage.length; j++) {


              total += stage[j].incompleteCounter;


                  data.stage[i].incompleteTotal = stage[j].incompleteCounter;


      }


}



Please mark this response as correct or helpful if it assisted you with your question.

Thanks sanjivmeher!



I followed your code, but changed:   data.stage[i].incompleteTotal = stage[j].incompleteCounter; to data.stage[i].incompleteTotal = total and it works!   Thanks!





Brian Dailey1
Kilo Sage

Hi David,



Where have you defined "data.stage" as an array (e.g., "data.stage = [];") ?   It sounds as though perhaps you haven't declared it to be an array [], and so by default it is treating "data.stage" as an object {} instead.   I think that is why you get the error 'Cannot set property "push"'... it is acting as though you are adding a new property called "push" through dot notation.




Hopefully that helps.



-Brian




Edit:   The "undefined" part of the error also makes it sound as though you haven't first declared what "stage" is to be, in relation to "data".   (i.e., "stage" is undefined)