NOTE: This seems a basic issue to me, but I did search for a solution to no avail. If an answer to this already exists, please kindly point me there - thank you!
I have json data received by a webhook node. One data in the json is a URL. The URL contains an ID that I want to extract (substr() ?) and pass to the next node.
My challenge is two-faced:
In the Functions node, I cannot figure out how to reference data from a previous node, although I otherwise built the JS function I need.
In the Expression feature, I cannot figure out how to perform JS functions, although I do know how to reference previous node data there.
If I can solve either of these challenges, I think I will have my solution.
I understand automation and workflows and I have an average understanding of JS. Connecting JS with n8n is still unknown territory for me. Please help
EP
EDIT Solution: I was trying to return a string from JS function but needed to return json. Also, the FunctionItem node was more useful in this case, and referencing previous nodes follows this pattern: $node[āName of Source Nodeā].json[āName of Dataā]
In the Function node you can read the incoming data via items. You can also use other expressions such as $items() which allow you to pick which node you want to read data from. Check out our documentation on these expressions: Methods - n8n Documentation
Iāve also attached a small workflow below showing you how to read data from other nodes in the Function node:
As for using JS inside an expression, this is a bit limited because of the underlying library. Basic stuff should however work, for example:
Hope this helps! Give me a shout if you have any questions on this.
Just a function node but then easier to use if you want to run it for each item in your workflow.
No need to keep in mind the json structure with this one.
I did try the FunctionItem Node but my use case doesnāt involve iterating through a data set, rather just extracting one string from a URL (substring from a string).
I think this is an issue of me learning how to use n8n for this use case, but unfortunately the docs are not yet detailed and tutorials are difficult to findā¦
The expression editor should do the trick for that.
and if you cannot get that to work the function Item node should do the trick without needing to worry about the structure of an item. (so pretty much how you have your function set up now can be pasted in function item)
Iām not trying to be disrespectful I just donāt know what Iām doing here. None of these nodes ādo the trickā because Iām not using them correctly.
If someone can show me how I can implement the following script to extract a piece of a string, that would be extremely helpful. And then I can extrapolate the knowledge into other areas and use cases.
I have searched this forum, I have scoured the docs, and Iāve spent time in YouTube videos. Iām here because I didnāt find any good introductory tutorial that shows how to correctly implement JS in n8n.
The bottom of this page suggests it is not possible to use advanced Javascript in the Expression feature.
Have you tried the Function Item node suggested by @BramKn further up? Thatās a bit simpler than the Function node as it only looks at your current item.
That said, the example data from your Webhook node screenshot looks very unusual - did you perhaps manually overwrite it? If so, you might need to adjust the code accordingly to reference your real-world data. For example, if your data is sent through under body like below, the first line of the code would have to be const myURL = item.body["Attachment URL"];
There are lot of different ways. Iād usually suggest using a Merge node in pass through mode to get whatever data you need, but as for the code route hereās an example:
Replace Mock Data in the Function Item code with the name of the node you want to retrieve your item from.
I think thatās how I originally had mine structured earlier, but perhaps in a Function node, and not Function Item. And in any case I was trying to return a string outside of json.
I suppose the JS in a node must always return jsonā¦or?
Hopefully some other newbs like me will get some value out of this. Thanks a lot @MutedJam and @BramKn - big help today!
Yep, n8n requires a specific JSON data structure. The Function Item node abstracts quite a bit of it, but if youāre curious, hereās Harshil explaining the basic concept:
This is a helpful vid - thank you for sharing this.
Also I want to add one small detail thatās probably a non-issue to most but still tripped me up for a sec:
After building code (correctly) in the FunctionItem node, the node itself needs to be executed once before the returned result can be accessed further down the workflow. Attempting to reference that FunctionItem nodeās data before it was executed will pull the raw JS code, rather than just the returned json. Obviously this is not what we want, so execute it once.
And that the index does not work in the expression editor didnāt work, I did not know.
Iāve now had the chance to actually check what you were doing and got a way to use it in the expression editor by using split and slice.
So that is still an option if you want it.
Happy to have helped, although @MutedJam did most of the heavy lifting
ps. if you simply want to add the value to the item object you can also do: