{"record":{"id":"fc1cdd1146c532ec","repo":"karatelabs/karate","slug":"array-from-requires-an-iterable-or-array-like-object-not","errorCode":null,"errorMessage":"Array.from requires an iterable or array-like object, not ","messagePattern":"Array\\.from requires an iterable or array-like object, not ","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsArrayConstructor.java","lineNumber":66,"sourceCode":"\n    private void installIntrinsics() {\n        defineOwn(\"from\", new JsBuiltinMethod(\"from\", 1, this::from), METHOD_ATTRS);\n        defineOwn(\"isArray\", new JsBuiltinMethod(\"isArray\", 1, (JsInvokable) this::isArray), METHOD_ATTRS);\n        defineOwn(\"of\", new JsBuiltinMethod(\"of\", 0, (JsInvokable) this::of), METHOD_ATTRS);\n        defineOwn(\"prototype\", JsArrayPrototype.INSTANCE, PropertySlot.INTRINSIC);\n    }\n\n    @Override\n    public Object call(Context context, Object[] args) {\n        return JsArray.create(args);\n    }\n\n    // Static methods\n\n    @SuppressWarnings(\"unchecked\")\n    private Object from(Context context, Object[] args) {\n        if (args.length == 0 || args[0] == null || args[0] == Terms.UNDEFINED) {\n            throw JsErrorException.typeError(\"Array.from requires an iterable or array-like object, not \" + (args.length == 0 ? \"undefined\" : args[0]));\n        }\n        Object source = args[0];\n        JsCallable mapFn = (args.length > 1 && args[1] instanceof JsCallable) ? (JsCallable) args[1] : null;\n        List<Object> results = new ArrayList<>();\n        // Iterable path: anything with @@iterator (built-in or user). Per spec, this\n        // takes priority over the array-like length-walk fallback.\n        JsIterator iter = IterUtils.tryGetIterator(source, context);\n        if (iter != null) {\n            int index = 0;\n            while (iter.hasNext()) {\n                Object v = iter.next();\n                Object mapped = mapFn == null ? v : mapFn.call(context, new Object[]{v, index});\n                results.add(mapped);\n                index++;\n            }\n            // Spec §23.1.2.1 returns an Array exotic — wrap so\n            // `Array.from(...).constructor === Array` and\n            // `Array.from(...) instanceof Array` hold.","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsArrayConstructor.java#L48-L84","documentation":"Array.from requires its first argument to be an iterable (has @@iterator) or an array-like object (has a usable length). Passing null, undefined, or no argument at all throws this TypeError. Note the message concatenates the offending value, so calling with zero args reads 'not undefined'.","triggerScenarios":"Array.from() with no args; Array.from(null); Array.from(undefined); Array.from on a value that is neither iterable nor array-like reaches this same throw when args[0] is null/undefined.","commonSituations":"Passing a possibly-null variable (e.g. a missing API response) straight into Array.from; forgetting that plain objects without length/iterator are not array-like; query results that came back undefined.","solutions":["Check the argument is non-null/non-undefined before calling Array.from","Convert plain objects with Object.values/Object.entries or Array.of for literal values","Provide a default (Array.from(x || [])) when the source may be absent"],"exampleFix":"// before\nArray.from(response.items); // items undefined -> TypeError\n// after\nArray.from(response.items || []);","handlingStrategy":"type-guard","validationCode":"function safeArrayFrom(x) { return x == null ? [] : Array.from(x); }","typeGuard":"function isIterableOrArrayLike(x) { return x != null && (typeof x[Symbol.iterator] === 'function' || (typeof x.length === 'number' && x.length >= 0)); }","tryCatchPattern":"try { var a = Array.from(src); } catch (e) { if (String(e).indexOf('Array.from requires') !== -1) { a = []; } else { throw e; } }","preventionTips":["Default nullish sources: Array.from(x || [])","Check that plain objects are converted with Object.values first","Never pass results of nullable lookups directly to Array.from"],"tags":["javascript","array","type-error","iterable"],"backgroundTag":"invalid-argument-value","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}