{"record":{"id":"d500dcadb8a1fd7e","repo":"karatelabs/karate","slug":"iterator-name-is-not-a-function","errorCode":null,"errorMessage":"iterator.${name} is not a function","messagePattern":"iterator\\.(.+?) is not a function","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/Interpreter.java","lineNumber":3049,"sourceCode":"            sent = sig.value;\n        }\n    }\n\n    /** Invoke {@code iterObj[name](arg)} with {@code iterObj} as `this`.\n     *  Spec GetMethod semantics: a nullish member is absent (TypeError when\n     *  {@code required}, null return otherwise — caller decides); a PRESENT\n     *  non-callable member is always a TypeError. A null {@code arg} means\n     *  call with no arguments. Cooperative errors surface via\n     *  context.isStopped(). */\n    private static Object invokeIteratorMethod(ObjectLike iterObj, String name, Object arg,\n                                               CoreContext context, boolean required) {\n        Object fn = iterObj.getMember(name, iterObj, context);\n        if (context.isStopped()) {\n            return Terms.UNDEFINED;\n        }\n        if (fn == null || fn == Terms.UNDEFINED) {\n            if (required) {\n                throw JsErrorException.typeError(\"iterator.\" + name + \" is not a function\");\n            }\n            return null;\n        }\n        if (!(fn instanceof JsCallable callable)) {\n            throw JsErrorException.typeError(\"iterator.\" + name + \" is not a function\");\n        }\n        Object result = invokeIteratorCallable(iterObj, callable, arg, context);\n        if (!context.isStopped() && required && !(result instanceof ObjectLike)) {\n            throw JsErrorException.typeError(\"iterator result is not an object\");\n        }\n        return result;\n    }\n\n    private static Object invokeIteratorCallable(ObjectLike iterObj, JsCallable callable,\n                                                 Object arg, CoreContext context) {\n        Object savedThis = context.thisObject;\n        context.thisObject = iterObj;\n        try {","sourceCodeStart":3031,"sourceCodeEnd":3067,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/Interpreter.java#L3031-L3067","documentation":"Karate resolves an iterator method (next/return/throw) by name from the iterator object. When required (e.g. next()) and the property is missing or undefined, it throws 'iterator.<name> is not a function'. This enforces the iterator protocol's minimum shape before invoking the method.","triggerScenarios":"Passing an object without a next() method where an iterator is expected (for-of, spread, IterUtils.getIterator adapters for strings/lists/Java iterables), or an iterator whose next was deleted or renamed.","commonSituations":"Typing mistakes like nex() or Next(); destructuring an iterator and passing the extracted next as a bound-less property; passing plain objects or Java collections through paths that expect protocol-shaped iterators.","solutions":["Ensure the object passed where an iterator is expected has a callable next() method","Pass the iterable itself (with Symbol.iterator) rather than a hand-shaped object, letting the engine obtain a correct iterator","If iterating Java collections or arrays, use the engine's supported forms (for-of over the collection) instead of a custom wrapper"],"exampleFix":"// before\nfor (const x of { next: myNext }) {...} // next not callable/undefined\n// after\nfor (const x of makeIterable()) {...} // object with [Symbol.iterator](){ return { next: () => ({done,value}) } }","handlingStrategy":"validation","validationCode":"if (!it || typeof it.next !== 'function') throw new TypeError('expected an iterator with next()');","typeGuard":"function isIterator(x) { return x != null && typeof x.next === 'function'; }","tryCatchPattern":"try { for (const v of it) {...} } catch (e) { if (String(e).includes('iterator.next is not a function')) { /* obtain proper iterator via Symbol.iterator */ } }","preventionTips":["Pass iterables (with Symbol.iterator), not ad-hoc objects, into iteration constructs","Check typeof it.next === 'function' before manual iteration","Rename conflicting data properties on iterator-like objects"],"tags":["javascript","iterator","typeerror"],"backgroundTag":"missing-required-argument","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"}