0

I am running node with cheerio. I have this function which needs to display an attribute value of an element contained in an array.

for (var row = 0; row < array.length; row++) {
    console.log("Row Length: " + array[row].length);
    for (var col = 0; col < array[row].length; col++) {
        console.log("Inside:");
        console.log(array[row][col].getAttribute('val'));        
    }
}

My array contains list items populated using push

array.push($('[title!="CATA"][collection="items"]'));

I get the desired output for 'Row Length', and it displays 'Inside', but then displays 'TypeError: undefined is not a function'. I have tried to use .attr('val') too but with the same error.

Executing console.log(array[row][col]) gives me the following.

{ type: 'tag',
  name: 'li',
  attribs: 
   { id: 'Amber',
     collection: 'items',
     title: 'disk',
     val: '10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8' },
  children: 
   [ { data: 'disk : 10|9|2|10|7|13|2|10|12|2|2|5|3|3|5|2|8|7|5|10|4|6|6|3|4|6|2|8|11|2|10|7|9|9|10|8',
       type: 'text',
       next: null,
       prev: null,
       parent: [Circular] } ],
  next: 
   { type: 'tag',
     name: 'li',
     attribs: 
      { id: 'Amber',
        collection: 'items',
        title: 'usb',
        val: '3|3|2|4|2|4|3|3|3|13|5|13|2|13|2|5|5|3|4|3|6|3|2|2|5|13|2|3|2|13|2|13|2|2|13|4' },
     children: [ [Object] ],
     next: 
     .
     .
     .

All I am trying to do is to access the 'val'.

NeelDeveloper
  • 139
  • 1
  • 15
  • Did you print array[0][0] element? You are experting HTML element, but I doubted array not holding HTML element at array[0][0] index. – venkat7668 Aug 13 '15 at 11:20

4 Answers4

1

I think, that the error is produced by this line

console.log(array[row][col].getAttribute('val'));

You are calling .getAttribute on a non-object. Give a full listing.

kiko
  • 72
  • 3
  • I appreciate your suggestion. I am trying to get the JQuery code to work on node with cheerio. I understand that cheerio and JQuery are different, but I need some help with accessing the attribute of an element stored in an array. – NeelDeveloper Aug 13 '15 at 11:27
  • @NeelDeveloper You're going to have to supply some example HTML code and what you want your selector to actually pick out if that is the help you are looking for. – Peadar Doyle Aug 13 '15 at 11:31
1

SOLVED:

console.log(array[row][col].attribs.val);
NeelDeveloper
  • 139
  • 1
  • 15
0

One or more values in your array is undefined and when you call getAttribute on it the error TypeError: undefined is not a function is thrown. To debug this look at the data inside the array by using console.log(array) or using a debugger. Ultimately the jQuery function is populating the array with unassigned variables so review the selector and see if it doing what you expect it to.

Peadar Doyle
  • 1,064
  • 2
  • 10
  • 26
0

First, print a more useful message:

if (!array[row][col] || !(array[row][col].getAttribute)) {
    console.log(row, col, array[row][col]);
}

Now look on your console and you'll see the indices for which your array is not filled with the kinds of objects you expect, as well as those objects.

Milimetric
  • 13,411
  • 4
  • 44
  • 56