{"record":{"id":"6919104f4a6f4f19","repo":"grafana/k6","slug":"a-function-is-expected-as-the-second-argument-of-s","errorCode":null,"errorMessage":"a function is expected as the second argument of SharedArray's constructor","messagePattern":"a function is expected as the second argument of SharedArray's constructor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/data/data.go","lineNumber":93,"sourceCode":"\trt := d.vu.Runtime()\n\n\tif d.vu.State() != nil {\n\t\tcommon.Throw(rt, errors.New(\"new SharedArray must be called in the init context\"))\n\t}\n\n\tname := call.Argument(0).String()\n\tif name == \"\" {\n\t\tcommon.Throw(rt, errors.New(\"empty name provided to SharedArray's constructor\"))\n\t}\n\tval := call.Argument(1)\n\n\tif common.IsAsyncFunction(rt, val) {\n\t\tcommon.Throw(rt, errors.New(asyncFunctionNotSupportedMsg))\n\t}\n\n\tfn, ok := sobek.AssertFunction(val)\n\tif !ok {\n\t\tcommon.Throw(rt, errors.New(\"a function is expected as the second argument of SharedArray's constructor\"))\n\t}\n\n\tbuilder := func() (sharedArray, error) { return getSharedArrayFromCall(rt, fn) }\n\tarray, err := d.shared.loadOrStore(name, builder)\n\tif err != nil {\n\t\tcommon.Throw(rt, err)\n\t}\n\n\treturn array.wrap(rt).ToObject(rt)\n}\n\n// RecordReader is the interface that wraps the action of reading records from a resource.\n//\n// The data module RecordReader interface is implemented by types that can read data that can be\n// treated as records, from data sources such as a CSV file, etc.\ntype RecordReader interface {\n\tRead() (any, error)\n}","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/grafana/k6/blob/01ffac6f245854c1b8adc6a69857c76a15f90022/internal/js/modules/k6/data/data.go#L75-L111","documentation":"The second argument to new SharedArray must be a function that k6 invokes to build the contents. After ruling out async functions, the constructor calls sobek.AssertFunction on the value; if it is not callable (an array, string, object, undefined...), it throws this error before anything is stored.","triggerScenarios":"'new SharedArray(\"name\", [1,2,3])' (passing the array directly), 'new SharedArray(\"name\", data)' where data is a parsed object, or omitting the second argument so call.Argument(1) is undefined.","commonSituations":"Assuming SharedArray wraps an existing array value like a normal constructor; migrating from a const data = [...] and passing the variable; forgetting the builder-function syntax in the first attempt with the API.","solutions":["Wrap the data in a function: new SharedArray('name', () => myArray).","If the data lives in a JSON file, use a builder that reads and parses it: () => JSON.parse(open('./data.json')).","Remember the returned value must itself be an array (see the 'only arrays' error) - the function wrapper is not optional."],"exampleFix":"// before\nconst arr = new SharedArray('ids', [1, 2, 3]); // throws\n\n// after\nconst arr = new SharedArray('ids', () => [1, 2, 3]);","handlingStrategy":"type-guard","validationCode":"if (typeof builder !== 'function') {\n  throw new Error(`SharedArray second argument must be a function, got ${typeof builder}`);\n}\nconst arr = new SharedArray(name, builder);","typeGuard":"const isCallable = (v) => typeof v === 'function';","tryCatchPattern":null,"preventionTips":["Use the canonical form: new SharedArray('name', () => [...]).","If linters are available, flag non-function second arguments at review time."],"tags":["k6","sharedarray","validation","constructor","type-error"],"backgroundTag":"constructor-argument-validation","analyzedSha":"01ffac6f245854c1b8adc6a69857c76a15f90022","analyzedAt":"2026-08-18T03:05:52.393Z","contentChangedAt":"2026-08-18T03:05:52.393Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}