sharedArray error: TypeError: Value is not an object

I must miss something fundamental here…

When i read in from data file the default way:
var data = open('./report_payload_TEST.json').split('\n');
and use the array referring to it’s elements by indexes, my test script works fine.

If i try to read in the same file as sharedArray:
var data = new SharedArray("some data name", function() { return open('./report_payload_TEST.json').split('\n'); })
i get the following error from k6:
level=error msg="TypeError: Value is not an object: undefined at file:///mnt/mono_test.js

What am i doing wrong?

Hello!

That’s two different JSON files (report_payload_TEST.json vs sensor_report_payload_TEST.json); are you sure the latter exists?

You may also be better off reading the JSON as an actual JS object rather than an array of strings using JSON.parse() (example in the docs here).

Good catch, but the file name difference was just a typo. So it is the same in both cases.

Regarding parsing the file as JSON, probably the extension of this file is missleading. Every line in this JSON is a complex JSON itself. I’m using it to store payloads for POST requests, so the script read 1 line from this file as a payload(JSON object) for every request.

Hi @bgyomorei,

running

import { SharedArray } from "k6/data";
var data = new SharedArray("some data name", function() { return open('./report_payload_TEST.json').split('\n'); })

export default function () {
    for (let a of data) {
        console.log(a);
    }
}

with the json file containing

{"a":5}
{"b":6}

produces

$ k6.v0.31.1 run forum-1609.js

          /\      |‾‾| /‾‾/   /‾‾/
     /\  /  \     |  |/  /   /  /
    /  \/    \    |     (   /   ‾‾\
   /          \   |  |\  \ |  (‾)  |
  / __________ \  |__| \__\ \_____/ .io

  execution: local
     script: forum-1609.js
     output: -

  scenarios: (100.00%) 1 scenario, 1 max VUs, 10m30s max duration (incl. graceful stop):
           * default: 1 iterations for each of 1 VUs (maxDuration: 10m0s, gracefulStop: 30s)

INFO[0000] {"a":5}                                       source=console
INFO[0000] {"b":6}                                       source=console
INFO[0000]                                               source=console

running (00m00.0s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     data_received........: 0 B 0 B/s
     data_sent............: 0 B 0 B/s
     iteration_duration...: avg=222.16µs min=222.16µs med=222.16µs max=222.16µs p(90)=222.16µs p(95)=222.16µs
     iterations...........: 1   875.401481/s

So it works. The extra line is because the last line has \n at the end so it splits with an extra empty line at the end.

Can you try to run only that code with your file and try to see if that will work. If it doesn’t please cut the JSON file down as much as possible and still reproducing the problem and open an issue (or post it here).

Also, you should have a line number where this happens … are you sure it is SharedArray related ?

Thanks a lot for looking into this!

I only get the error when i define my data variable with sharedArray, so that’s why i think the problem is with that.

I’ve tried my data file with your example code and it worked. Using the very same sharedArray definition in my code, i get the error.

This is the full error log i get:
time="2021-04-08T12:08:01Z" level=error msg="TypeError: Value is not an object: undefined at file:///mnt/mono_test.js:64:12(71)"
My problem with this is that the whole script is less than 50 lines, so “64:12(71)” has no information for me atm.

This is the full (slightly sanitized) script:
import http from ‘k6/http’;
import { check, sleep, SharedArray } from ‘k6’;

/* k6 specific parameters
*/

export let options = {
    vus: 6,
    duration: '1s',
  };

// Parameter to control request sending interval (seconds)
var sampling_interval = 1

// Parameters to create request URL and payload
//var data = open('./sensor_report_payload_TEST.json').split('\n');
var data = new SharedArray("some data name", function() { return open('./report_payload_TEST.json').split('\n'); })
var url_data = open('report_url_TEST.csv').split('\n');
var host = "https://www.example.com"
var year = "2021"
var month = "03"
var day = "29"

export default function () {
    let payload_index = (__ITER * options.vus) + (__VU - 1)
    let payload = data[payload_index];
    let url_params = url_data[payload_index];
    let processed_url_params = url_params.split(',');
    let group = "testgroup";
    let uid = "fakestamp-" + group + "-" + processed_url_params[1]
    let url = host + '/some/endpoint/' + processed_url_params[0] + '/' + year  + '/' + month + '/' + day + '/' + uid

    console.log('url[' + __VU + '-' + __ITER + '-' + payload_index + ']: ' + url);
    console.log('payload[' + __VU + '-' + __ITER + '-' + payload_index + ']: ' + payload)

    //let params = { headers: { "Accept": "application/json", "Content-Type": "application/json" } };
    //let res = http.post(url, JSON.stringify(body), params);

    //console.log('Response was: ' + String(res.request.url));

     sleep(sampling_interval);
}

Maybe referencing for an element of sharedArray like let payload = data[payload_index]; doesn’t work?

Side question, how could i format my code as cool as you did? I only have this preformatted option in the message editor…

You need to import SharedArray from k6/data not k6 that is the line that is actually blowing up with the not so … helpful message as SharedArray is undefined.

Side question, how could i format my code as cool as you did? I only have this preformatted option in the message editor…

The forum supports some Markdown and I am wrapping it in triple "" and adding javascript` to the end of the first wrap as in

```javascript
code here
```

Which apparently is called Fenced Code Blocks

1 Like

Thank you! It is working!