Need help with pre-request script

Hello everyone,

for some reason I cannot get my pre-request script to work. It is suppposed to read some variables from a csv file and do some data wrangling to change my request every time.

Here’s the code:

var names = pm.variables.get("Artikel").split(",");
var sku = sku.iterationData.get("Artikelnummer").split(",");
var prices = pm.iterationData.get("Preis").split(",");
var line_items = [];

for (let i = 0; i < names.length; i++) {
    var line_item = { variant_id: null, price: prices[i], name: names[i], sku: sku[i],title: names[i],quantity: 1};

//var line_items_json = JSON.stringify(line_items);
//pm.environment.set("lineItems", line_items_json );
pm.environment.set("lineItems", line_items )

I get the following error: TypeError: Cannot read properties of undefined (reading ‘iterationData’). Strangely enough though, the console.log works and prints the data to the console. Afterwards the script stops executing though. Am I doing something wrong?

Thanks very much for your help!

Nevermind, got it to work. The problem was using “var” instead of “let” to initialize variables.

Pretty sure that this would have been your issue, not whether the variables were declared using var or let (which are both acceptable ways to declare your variables).

Sku would have been undefined, therefore iterationData would not exist for that variable hence the error “Cannot read properties of undefined (reading ‘iterationData’)

I really wish Postman would tell you which line it was erroring on.

It should probably be pm.iterationData.

Are you also aware that you can also retrieve the CSV data by using “data.columnName” instead of pm.iterationData?

let sku = data.Artikelnummer // or
let sku = data["Artikelnummer"]

Using CSV and JSON Data Files in the Postman Collection Runner | Postman Blog

Thank you. I guess these types of issues happen when I try to write code when I’m too tired.

1 Like