{"record":{"id":"3e8dbb8f0161ed0e","repo":"n8n-io/n8n","slug":"union-expected-array-arg-e-g-union-1-2-3","errorCode":null,"errorMessage":"union(): expected array arg, e.g. .union([1, 2, 3, 4])","messagePattern":"union\\(\\): expected array arg, e\\.g\\. \\.union\\(\\[1, 2, 3, 4\\]\\)","errorType":"exception","errorClass":"ExpressionExtensionError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/expression-runtime/src/extensions/array-extensions.ts","lineNumber":272,"sourceCode":"\tconst listLength = value.length > others.length ? value.length : others.length;\n\tlet merged = {};\n\tfor (let i = 0; i < listLength; i++) {\n\t\tif (value[i] !== undefined) {\n\t\t\tif (typeof value[i] === 'object' && typeof others[i] === 'object') {\n\t\t\t\tmerged = Object.assign(\n\t\t\t\t\tmerged,\n\t\t\t\t\tmergeObjects(value[i] as Record<string, unknown>, [others[i]]),\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t}\n\treturn merged;\n}\n\nfunction union(value: unknown[], extraArgs: unknown[][]): unknown[] {\n\tconst [others] = extraArgs;\n\tif (!Array.isArray(others)) {\n\t\tthrow new ExpressionExtensionError('union(): expected array arg, e.g. .union([1, 2, 3, 4])');\n\t}\n\tconst newArr: unknown[] = Array.from(value);\n\tfor (const v of others) {\n\t\tif (newArr.findIndex((w) => isEqual(w, v)) === -1) {\n\t\t\tnewArr.push(v);\n\t\t}\n\t}\n\treturn unique(newArr, []);\n}\n\nfunction difference(value: unknown[], extraArgs: unknown[][]): unknown[] {\n\tconst [others] = extraArgs;\n\tif (!Array.isArray(others)) {\n\t\tthrow new ExpressionExtensionError(\n\t\t\t'difference(): expected array arg, e.g. .difference([1, 2, 3, 4])',\n\t\t);\n\t}\n\tconst newArr: unknown[] = [];","sourceCodeStart":254,"sourceCodeEnd":290,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/expression-runtime/src/extensions/array-extensions.ts#L254-L290","documentation":"Thrown as ExpressionExtensionError by the .union() array extension when its argument is not an array. union() concatenates the base array with another array and removes duplicates; a non-array argument cannot be iterated for the concatenation step, so it is rejected.","triggerScenarios":"Calling .union() with a non-array: .union(5), .union('abc'), .union({a:1}), or .union() where the harness passes undefined (undefined fails Array.isArray). The expected form is .union([1,2,3,4]).","commonSituations":"Passing a scalar or object instead of an array. A template variable resolving to a single value rather than a list. Forgetting the brackets around the argument.","solutions":["Pass an array: .union([1,2,3,4]).","If the argument is dynamic, ensure it is an array before calling: Array.isArray(arg) ? .union(arg) : .union([arg]).","Coerce single values into a one-element array when a union with a scalar is intended.","Confirm the upstream expression yields an array."],"exampleFix":"// before — scalar argument\n{{ $json.tags.union($json.moreTags) }} // if moreTags is a string, throws\n// after — ensure array argument\n{{ $json.tags.union(Array.isArray($json.moreTags) ? $json.moreTags : [$json.moreTags]) }}","handlingStrategy":"type-guard","validationCode":"function unionSafe(arr, others) {\n  if (!Array.isArray(others)) throw new TypeError('union(): expected array arg');\n  return [...new Set([...arr, ...others])];\n}","typeGuard":"function isUnionArgValid(others) { return Array.isArray(others); }","tryCatchPattern":"try {\n  result = arr.union(others);\n} catch (e) {\n  if (e.name === 'ExpressionExtensionError' && /union.*array/i.test(e.message)) {\n    // ensure others is an array: arr.union(Array.isArray(others) ? others : [others])\n  }\n}","preventionTips":["Always pass an array: .union([1,2,3,4]).","Coerce scalars: .union(Array.isArray(x) ? x : [x]).","Guard with Array.isArray(arg) before calling.","Confirm upstream expressions that produce the argument yield an array."],"tags":["expression-runtime","array-extensions","union","argument-validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}