{"record":{"id":"45b48bebda72001c","repo":"karatelabs/karate","slug":"iterator-next-is-not-a-function","errorCode":null,"errorMessage":"iterator.next is not a function","messagePattern":"iterator\\.next is not a function","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/IterUtils.java","lineNumber":204,"sourceCode":"    }\n\n    /**\n     * Walk an already-constructed iterator object's {@code .next()} method to\n     * produce a {@link JsIterator}. Shared by {@link #userIterator} (which\n     * first invokes {@code @@iterator}) and the spec-shaped branch of\n     * {@link #iteratorFromCallable}.\n     */\n    private static JsIterator iteratorObjectWalker(ObjectLike iterObj, Context context) {\n        return new JsIterator() {\n            Object pending;\n            boolean fetched;\n            boolean done;\n\n            private void fetch() {\n                if (fetched || done) return;\n                Object nextFn = iterObj.getMember(\"next\");\n                if (!(nextFn instanceof JsCallable nextCallable)) {\n                    throw JsErrorException.typeError(\"iterator.next is not a function\");\n                }\n                Object step;\n                if (context instanceof CoreContext cc) {\n                    Object savedThis = cc.thisObject;\n                    cc.thisObject = iterObj;\n                    try {\n                        step = nextCallable.call(cc, EMPTY_ARGS);\n                    } finally {\n                        cc.thisObject = savedThis;\n                    }\n                } else {\n                    step = nextCallable.call(context, EMPTY_ARGS);\n                }\n                if (isJsErrored(context)) { done = true; return; }\n                if (!(step instanceof ObjectLike stepObj)) {\n                    throw JsErrorException.typeError(\"iterator result is not an object\");\n                }\n                if (Terms.isTruthy(readMember(stepObj, \"done\", context))) {","sourceCodeStart":186,"sourceCodeEnd":222,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/IterUtils.java#L186-L222","documentation":"In the iterator-object wrapper built from a user-supplied iterator object, fetch() reads the `next` member and requires it to be a callable. If the wrapped object's `next` property is missing, null, or not a function, karate throws this TypeError — the object claimed to be an iterator but does not satisfy the iterator protocol.","triggerScenarios":"An object returned from Symbol.iterator has no `next` member or `next` is a non-callable value (e.g. a plain data object `{done:false, value:1}` returned instead of `{next: fn}`), consumed via for-of/spread.","commonSituations":"Confusing an iterator RESULT object ({value,done}) with an ITERATOR object ({next}), returning the wrong shape from a custom Symbol.iterator, or a typo'd member name (`nxt`, `next()` assigned result).","solutions":["Return an object whose `next` is a function: `{ next: function() { return {value: v, done: b}; } }`.","Do not return a step result object directly as the iterator.","If next is optional in some states, still provide it (e.g. always return `{done:true}` steps)."],"exampleFix":"// before\nreturn { value: 1, done: false };\n// after\nreturn { next: function() { return { value: 1, done: false }; } };","handlingStrategy":"validation","validationCode":"var it = obj[Symbol.iterator]();\nif (it == null || typeof it.next !== 'function') throw new TypeError('object is not an iterator');","typeGuard":"function isIterator(o) { return o != null && typeof o.next === 'function'; }","tryCatchPattern":"try { for (var x of obj) { ... } } catch (e) { if (String(e).indexOf('next is not a function') !== -1) { /* use manual index-based loop */ } else { throw e; } }","preventionTips":["Never return a result-step object where an iterator is expected","Assign function references (no call parentheses) to next","Unit-test the iterator object shape before relying on it"],"tags":["javascript","iterator-protocol","typeerror"],"backgroundTag":"unexpected-response-shape","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"}