I'm having trailing '\n' (new-lines) and spaces after expressions and can't get rid of them

Describe the problem/error/question

I’m using Javascript expressions to transform data, but I’m getting trailing new-lines after a node in a flow. I’m using this expression:

{{
(
$(‘MySQL’).first().json.assetType === ‘assets’ ? ‘comm_’ :
$(‘MySQL’).first().json.assetType === ‘crypto’ ? ‘crypto_’ :
$(‘MySQL’).first().json.assetType === ‘metals’ ? ‘metals_’ :
$(‘MySQL’).first().json.assetType === ‘currencies’ ? ‘curr_’ :
‘’
) + $(‘Number?2’).first().json.symbol.replace(/[\s\r\n]+/g, ‘’).toUpperCase()
}}

But the output of the node (in this case a PostgreSQL node that wants to update a record), is always ‘symbol\n’. Which results in an error that record (curr_BPX ) is not present in the table. Note the trailing space after the symbol-name (curr_BPX ). No matter the effort, I can’t seem to get rid of it.

What is the error message (if any)?

insert or update on table “PredictionManual” violates foreign key constraint “PredictionManual_assetSymbol_fkey”

Key (assetSymbol)=(crypto_BPX ) is not present in table “Asset”.

(While a check shows that crypto_BPX is in table ‘Asset’.)

Please share your workflow

CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

The workflow is 78000 characters and I can’t share that here. I’ve built many workflows though and all of them are working fine. It’s just that these trailing \n’s start popping up and sanitizing is not working.

Share the output returned by the last node

Last node doesn’t have an output as it’s a response to the webhook.

My expected output is the symbolname in capitals, with a prefix and no trailing whatschamacallit.

Information on your n8n setup

  • n8n version:

Latest, 1.69.2

  • Database (default: SQLite):

  • n8n EXECUTIONS_PROCESS setting (default: own, main):
    default

  • Running n8n via (Docker, npm, n8n cloud, desktop app):
    Docker

  • Operating system:

Ubuntu 22.04 server

Found it. For anybody running into the same problem: if you copy expressions from ChatGPT, for they can be menacingly difficult at times, then check if it doesn’t contain ‘\n’-s which it uses to display the expression in a readable fashoin, but these are not directly visible. If copied to the expressionfield of n8n they show the new-line, but not the ‘\n’ used for it and it copies the new-line from the expression, in this case after the ‘symbol’, in the output. If checked in JSON you see the ‘\n’-s and after removal no more strange traling new-lines!

So this topic is SOLVED!

2 Likes

Hey @Michel_Hageman,

Glad you managed to figure it out, I’ll mark your answer as the Solution :white_check_mark:

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