lc4t
May 26, 2021, 6:46pm
1
I check old topic already, but not one works.
the first node is MySQL query, it returns 3 rows, as array, and each row has 4 columns
[{
a: 1,
b: 1,
c: 1,
d: 1
},
{
a: 2,
b: 2,
c: 2,
d: 2
},
{
a: 3,
b: 3,
c: 3,
d: 3
}]
And next node is javascript code, I want to get COUNT OF ROWS, but failed:
Object.keys( items[0].json) is 4 but not 3;
Object.keys( $node[‘mysql’].json) is 4 but not 3;
items.length is 1;
BUT: I can get items by $item(2).$node['mysql'].json
, it returns the last row.
I cannot get count of rows, how to get it ?
Welcome to the community @lc4t
Object.keys( items[0].json) is 4 but not 3;
With this, you are counting the number of properties that item 0 has. Since you have 4 properties a,b,c,d it returns 4.
items.length is 1;
Are you using a function item node? because if so, then it makes sense. The function item node is executed 1 time per item.
I cannot get count of rows, how to get it ?
What you need to do is add a function node (NOT a function item node) after the MySQL node, with the following node.
return [ { json: { count: items.length } } ]
Also, you can query the number of items directly in the database.
For example:
SELECT count(*) FROM ‘your_table’ WHERE ‘your_condition’
1 Like
lc4t
May 27, 2021, 3:05am
3
thx, create a new function node after mysql node, [ { json: { count: items.length } } ]
works.
but,if I modify function node code, to handle my data, the items.length returns always 1, I donnot know why.
Hey @lc4t ,
You can save the value of items.length
in a variable and then manipulate the items array.
what do you want the function node’s output to be exactly?