I'm returning a number and want to check if it exists in my list of ids but I can't seem to get the value of the id

I’m returning a number and want to check if it exists in my list of ids but I can’t seem to get the value of the id

Describe the problem/error/question

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hello @nhanh102

You can’t return a single number with the Code node. You have to return an Object or Array of Objects

1 Like

@nhanh102 , your code has a few issues not to mention that you do not need to use Code node to achieve what you are looking for in the first place. However, assuming you have a reason to use Code node, here are the issues with your code.

  1. The expression $input.first().json returns an object (!). Therefore, looking for it in the string is not valid. That is, text.includes(searchText) will never work.
  2. The returned value has to be an array of objects (or just an object, which will be wrapped into an array automatically). However, you are trying to return a string with input[0].json.id. Thus, you are getting the error “Code didn’t return items properly”.

If you want to check if id’s value is in the string then you would need the include() method to have a string as its argument. That is, your searchText should have $input.first().json.id assigned.

Looks like you just want to return id. That could be achieved with return $input.first().

As I mentioned already, simply to check if id’s value is in the string, you do not need to use Code node. Depending on what you want to do next, IF node could be a better option. For example,

1 Like

Actually, I am returning an id but want to check if it is in a list of ids. If so, return true to continue. But if you do it this way, in case it returns 1 but the string has 10101, it still returns true.

Yep, Actually, I am returning an id but want to check if it is in a list of ids. If so, return true to continue. But if you do it this way, in case it returns 1 but the string has 10101, it still returns true.

There are many ways of how you can check it. Here are couple of examples

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