grafana/k6 · error
the argument to map() must be a function
Error message
the argument to map() must be a function
What it means
Selection.Map() in k6's HTML module implements ES5 Array.prototype.map over the matched elements and requires a single function argument; sobek.AssertFunction rejects everything else. Results of the callback are collected into a plain array of sobek values (callbacks that throw are silently skipped, their result omitted).
Source
Thrown at js/modules/k6/html/html.go:421
case Selection:
return s.sel.IsSelection(val.sel)
default:
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to is() must be a function, a selector or a selection"))
}
return s.sel.IsFunction(s.buildMatcher(v, sobekFn))
}
}
// Map implements ES5 Array.prototype.map
func (s Selection) Map(v sobek.Value) []sobek.Value {
sobekFn, isFn := sobek.AssertFunction(v)
if !isFn {
common.Throw(s.rt, errors.New("the argument to map() must be a function"))
}
var values []sobek.Value
s.sel.Each(func(idx int, sel *goquery.Selection) {
selection := &Selection{sel: sel, URL: s.URL, rt: s.rt}
if fnRes, fnErr := sobekFn(v, s.rt.ToValue(idx), s.rt.ToValue(selection)); fnErr == nil {
values = append(values, fnRes)
}
})
return values
}
func (s Selection) Slice(start int, def ...int) Selection {
// We are forced to check that def[0] is inferior to the length of the array
// otherwise the s.sel.Slice panics. Besides returning the whole array when
// the end value for slicing is superior to the array length is standard in js.View on GitHub (pinned to 93accf6570)
Solutions
- Pass a callback: sel.map((idx, el) => el.text())
- For selecting subsets first, chain .find() or .filter() before .map()
- Confirm the callback variable is defined at call time (typos yield undefined)
Example fix
// before
const texts = selection.map('div');
// after
const texts = selection.find('div').map((idx, el) => el.text()); Defensive patterns
Strategy: type-guard
Type guard
if (typeof fn !== 'function') throw new TypeError('map() expects a function');
const values = sel.map(fn); Prevention
- Always pass (idx, el) => ... to map()
- Verify callback variables are assigned (typos give undefined)
- Project data in the callback; do not pass selectors to map()
When it happens
Trigger: Calling map() with a selector string, no argument, or a non-callable value; expecting jQuery-style arguments without providing the callback.
Common situations: Attempting sel.map('div') to project elements (should be find + map); refactor that dropped the lambda; passing an arrow function stored in a variable that is actually undefined.
Related errors
- the argument to each() must be a function
- the argument to filter() must be a function, a selector or a
- the argument to is() must be a function, a selector or a sel
- Error while parsing selector `${selector}`: ${e.message}
- Error while parsing selector `${selector}` - cannot use ${op
AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15).
Data as JSON: /api/errors/602fdbee886680f3.
Report an issue: GitHub.