Function Node: Access output data from previous node

Good Morning guys,

quick question: I want to use a function node to parse output data from a previous node. However when I try to access the data and return it, I receive "Problem executing flow: “Always an Array of items has to be returned!”.

This is the output data from the Webhook Node:

    [{"body":{"lead": {"email": "[email protected]","ipAddress": "","referrer": ""},
    "campaign": {"id": "trffqzciuhhilbz5ceuh","title": "TEST-CAMPAIGN"}},
    "headers": {/* 16 items */ },"query": {/* no items */ }}]

This is the my code from the function node:

    const test = $node["Webhook"].json["body"]["lead"]["referrer"];
    return test;

Can someone point me in the right direction of how I can access the URL of the referrer?

Many thanks

It is probably best to check the documentation. The one of the function node can be found here:

And the important thing to know of the Function-Node is that it always has to return the full data. Meaning all the items (therefor an array) and the items have to be valid. Information about the data structure used in n8n can be found here:

The problem you have above is that you return a single value (probably a string) when you have to return items. So what you want to do is probably more along the lines of the very first example in the documentation and would so simply be adding the data to an already existing item like this:

items[0].json.test = $node["Webhook"].json["body"]["lead"]["referrer"];
return items;

If that is, however, the only thing you want to do it would probably be easier to use a Set-Node with an expression instead.

Thanks for pointing me in the right direction @jan. I will do some more reading on it. What I actually want to do is parse the countrycode out of the referrer URL (example would return “de”), and store it so I can use it in the following node. So I assume the Set-Node is not really of much help here.

Would also be possible in an Expression but is maybe really better in a Function-Node.