{"record":{"id":"ac8ec15ae2d84355","repo":"n8n-io/n8n","slug":"difference-expected-array-arg-e-g-difference","errorCode":null,"errorMessage":"difference(): expected array arg, e.g. .difference([1, 2, 3, 4])","messagePattern":"difference\\(\\): expected array arg, e\\.g\\. \\.difference\\(\\[1, 2, 3, 4\\]\\)","errorType":"exception","errorClass":"ExpressionExtensionError","httpStatus":null,"severity":"error","filePath":"packages/@n8n/expression-runtime/src/extensions/array-extensions.ts","lineNumber":286,"sourceCode":"\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[] = [];\n\tfor (const v of value) {\n\t\tif (others.findIndex((w) => isEqual(w, v)) === -1) {\n\t\t\tnewArr.push(v);\n\t\t}\n\t}\n\treturn unique(newArr, []);\n}\n\nfunction intersection(value: unknown[], extraArgs: unknown[][]): unknown[] {\n\tconst [others] = extraArgs;\n\tif (!Array.isArray(others)) {\n\t\tthrow new ExpressionExtensionError(\n\t\t\t'intersection(): expected array arg, e.g. .intersection([1, 2, 3, 4])',\n\t\t);","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/n8n-io/n8n/blob/5ac6606e81f67bb9534255570cd4e86fd8101eee/packages/@n8n/expression-runtime/src/extensions/array-extensions.ts#L268-L304","documentation":"Thrown as ExpressionExtensionError by the .difference() array extension when its argument is not an array. difference() returns elements in the base array that are not present in the other array; a non-array argument cannot be scanned for membership, so it is rejected up front.","triggerScenarios":"Calling .difference() with a non-array: .difference(5), .difference('abc'), .difference({a:1}), or with undefined where the harness passes a non-array. The expected form is .difference([1,2,3,4]).","commonSituations":"Passing a scalar or object instead of an array. A template variable resolving to a single value. Forgetting the brackets. Same shape as the union/intersection guards.","solutions":["Pass an array: .difference([1,2,3,4]).","If the argument is dynamic, ensure it is an array before calling: Array.isArray(arg) ? .difference(arg) : .difference([arg]).","Coerce single values into a one-element array when a difference with a scalar is intended.","Confirm the upstream expression yields an array."],"exampleFix":"// before — scalar argument\n{{ $json.ids.difference($json.excluded) }} // if excluded is a number, throws\n// after — ensure array argument\n{{ $json.ids.difference(Array.isArray($json.excluded) ? $json.excluded : [$json.excluded]) }}","handlingStrategy":"type-guard","validationCode":"function differenceSafe(arr, others) {\n  if (!Array.isArray(others)) throw new TypeError('difference(): expected array arg');\n  return arr.filter(x => !others.includes(x));\n}","typeGuard":"function isDifferenceArgValid(others) { return Array.isArray(others); }","tryCatchPattern":"try {\n  result = arr.difference(others);\n} catch (e) {\n  if (e.name === 'ExpressionExtensionError' && /difference.*array/i.test(e.message)) {\n    // ensure others is an array: arr.difference(Array.isArray(others) ? others : [others])\n  }\n}","preventionTips":["Always pass an array: .difference([1,2,3,4]).","Coerce scalars: .difference(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","difference","argument-validation"],"backgroundTag":null,"analyzedSha":"5ac6606e81f67bb9534255570cd4e86fd8101eee","analyzedAt":"2026-08-12T05:26:35.080Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}