I’m working on periodical data synchronization between LDAP catalog and MongoDB collection. The logic to do it very simple: at first, we need to insert/replace all existing LDAP user records in MongoDB and then delete from it records which no longer exist in LDAP.
To delete records, I need to pass a quoted list of UIDs to mongo json query. I try to do it with $jmespath function, but it looks n8n forcibly removes quotes from the $jmespath output (this behavior is adopted under another question):
At the same time, a preview of the expression shows the result that I need:
I understand that the question of expected behavior is debatable. Is there any way to get an array of quoted strings for JSON without explicit generation of it in the JS code node?
Everything between double-curly-brackets in the expression editor is considered javascript. The $jmespath()-function constructs an array. The array is embedded inside text:
"{ “uid”: { “$nin”: [ Array here ] } }
At some point, n8n seems to get confused, merging the two. In the preview, it extrapolated correctly, in the actual request, it’s probably doing something else (sanitization/…? haven’t looked at the code).
The above solution works, because it creates a json (text) representation of the array and embeds it into the surrounding text. Square brackets included.
For simple nodes, I now always make sure the expressions in my n8n workflows output text-only from the constructing javascript. For everything else, I anyway prefer the code node. No issues since.
tl;dr
Don’t embed a JS-Object into text that is part of a node expression. n8n might not do with it what you expect. Instead, toString() first.
Hope this helps!
ps: haven’t tested your exact case, ymmv. let me know.