Trouble with a simple function

I’m putting a switch statement in a function node. Getting the error:
ERROR: executionData.every is not a function [Line 731]
The preceding node gets an order object, “source_name” from Shopify, “Shopify Trigger”, that looks fine. I think I’m referencing the source node correctly. Stumped!

let namingSeries = "";
switch ($node["Shopify Trigger"].json["source_name"]) {
  case "pos":
    namingSeries = "POS-";
    break;
  case "web":
    namingSeries = "WEB-";
    break;
  default:
    namingSeries = "eBay-";
}
return namingSeries;

Thanks!
Mike

Hi @Mike_Z, welcome to the community :tada:

The problem will be your return value which is a string. n8n does however expect an array following a specific structure (since a few versions it can also handle generic objects, but no primitives yet).

So if you rewrite your Function code to follow n8n’s data structure the error should disappear, for example like so:

let namingSeries = "";
switch ($node["Shopify Trigger"].json["source_name"]) {
  case "pos":
    namingSeries = "POS-";
    break;
  case "web":
    namingSeries = "WEB-";
    break;
  default:
    namingSeries = "eBay-";
}
return [{
  json: {
    myResult: namingSeries
  }
}];

In n8n versions ≥0.166.0 you can alternatively do this instead:

let namingSeries = "";
switch ($node["Shopify Trigger"].json["source_name"]) {
  case "pos":
    namingSeries = "POS-";
    break;
  case "web":
    namingSeries = "WEB-";
    break;
  default:
    namingSeries = "eBay-";
}
return {
  myResult: namingSeries
};

image

Hope this helps! Let me know if you run into any trouble with this.

2 Likes

Thanks, @MutedJam ! I read and re-read the docs, which are perfectly clear, but I didn’t register that important bit. Can’t wait to get back to it now, though I’m off to a client’s site.
Mike

1 Like

That sounds very similar to how I felt when I first discovered n8n :smiley:

Glad to hear this helped, give me a shout if you run into any more trouble!

I guess the next question would be: is it possible to make the error message more descriptive? Like check the result data structure and throw an exception with an informative message?