Check Null Output javascript not working

Hi Expert,

i have copy the code from forum to check the null output of previous node, it is good when i use on the self-host version.

return [{json: {empty: items.length == 1 && Object.keys(items[0].json).length == 0}}];

However, i try to do the same thing on the desktop version, it is fails with below error.

i am not good in coding, can any expert help!! thanks

Hi @Louis_kwok, the items object doesn’t exist on the Code node. Is there a chance you were using the older Function node on your self-hosted instance of n8n?

The items equivalent (referencing all incoming items) in the Code node would be $input.all(). This expression is part of the example snippet in the Code node and also documented here.

So, in your Code node you might want to use a snippet like this, where items has been replaced with $input.all():

return [
    json: {
      empty: $input.all().length == 1 && Object.keys($input.all()[0].json).length == 0}

Example workflow:

Hope this helps!

I also stumbled several times over the items[0] in the quite minimalistic documentation and it took me hours as a beginner to figure that out.

Can you guys please clean up the docs and add some info in the forum-entries on that?
for example:

Hi @ourHDC, welcome to the community and sorry for the trouble.

When using the code node you might want to use $input.all().length as well, similar to what has been suggested above. Like so:

I’ve also flagged this docs page for an update.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.