Unable to understand 2 issues in a workflow of Requests

Hello,

I have a collection with 3 Requests.

image.png

Then I run only the first two of them using the Runner, and First I am getting a list of some projects from the first request with name (List projects Copy) and I can store them in a local environment using the following code in the Tests scripts

pm.test("Status code is 200", function () {

pm.response.to.have.status(200);

});

let response = pm.response.json();

let projects =[];

for (let i=0; i< response.data.length; i++) {

let proj_slug = response.data[i].attributes.slug;

// console.log(string1);

projects.push({

"projectslug":proj_slug

});

}

// Store all the PR stringhashes in the environment variable

pm.environment.set("projectslugs",JSON.stringify(projects));

// Store the next index of the array for PR string hash

pm.environment.set('nextIndex', -1);

// Store the active PR string hash id to fetch the comments for

pm.environment.set('pslug', response.data[0].attributes.slug);

//console.log(pm.variables.get('pslug'));

Then I execute the second request with name (Create a tmx file download action-Panos_All Languages), where it works and returns some data, but here I have 2 problems

I use code both to Pre-request Script and Tests Script.

Under the Pre request TAB I have the following code

//Set the value of the Environment Array variable projectslugs to

//local array projectslugsArray

let projectslugsArray = JSON.parse(pm.environment.get('projectslugs'));

//console.log(projectslugsArray);

// Increment the next Index

nextIndex = parseInt(pm.environment.get('nextIndex')) + 1;

// In case project's slugs have been fetched for all requests then we're done here time to end the collection run and clean up the environment and active shash

console.log('Length='+projectslugsArray.length+' - Nextindex:'+nextIndex);

if (projectslugsArray.length === (nextIndex)) {

pm.environment.set('nextIndex', 0);

pm.environment.set('pslug', projectslugsArray[0]);

postman.setNextRequest(null);

}

else {

let pslug1 = projectslugsArray[nextIndex];

pm.environment.set('nextIndex', nextIndex);

pm.environment.set('pslug', pslug1.projectslug);

//console.log('pslug');

console.log(pm.variables.get('pslug'));

// Now run the POST request to create a download Action for the TMX from a project slug that came from the Get List projects Request

postman.setNextRequest("Create a tmx file download action-Panos_All Languages");

}

Under the Tests TAB, I have the following code

pm.test("Status code is 202", function () {

pm.response.to.have.status(202);

});

let response = pm.response.json();

//console.log(response);

//console.log(response.data);

let projects_tmx_ids =[];

//projects_tmx_ids = pm.variables.get('projectstmxids');

// console.log(projects_tmx_ids);

console.log('Response Data Length:'+response.data.length);

//console.log(pm.environment.get('projectstmxids'));

let tmx_id = response.[data.id](http://data.id/);

console.log(pm.environment.get('pslug')+' - '+tmx_id);

projects_tmx_ids.push({

"id":tmx_id,

"status":"unknown"

});

// Store all the ids of the TMX post requests in the environment variable

pm.environment.set("projectstmxids",JSON.stringify(projects_tmx_ids));

// Store the next index of the array for TMX ids

pm.environment.set('nextIndexTMX', -1);

// Store the active TMX id to fetch the file for this.

pm.environment.set('tmxid', response.[data.id](http://data.id/));

//console.log(pm.variables.get('tmxid'));

Here my problem, is that I was expecting when I will finish the execution of the second request to have under the local variable projectstmxids

all the tmx_id with their status. However, it stores only the last one. See the next image from the Environment variables

image.png

Also, the second problem I have is that if I run only the 2 requests through the runner, I get the following output in the console log

image.png

I received in the console log 42 examples of the following outputs

Response Data Length:undefined

tx-native_django - c09da83b-baa4-42d9-9736-dd2fc4d63d2b

Length=42 - Nextindex:41

webhook_project

POST https://rest.api.transifex.com/tmx_async_downloads
202
148 ms

Response Data Length:undefined

webhook_project - 2847ce3d-a04a-4a85-83bc-aec0fd366523

Length=42 - Nextindex:42

POST https://rest.api.transifex.com/tmx_async_downloads
400
121 ms

And then I get the error

TypeError: Cannot read property ‘length’ of undefined

So, my second question here is why the execution stopped here with this error since for all the previous 41 iterations, I received the message “Response Data Length:undefined”

without any error.

I hope to share with you all the necessary information. Please let me know If I missed anything.

Kind regards,
Panagiotis

1 Like

Hi Panagiotis! It looks like the images you uploaded are broken. Do you mind re-adding them? This is a really well-worded question!

Hello @hannah.neil

Thank you for taking the time to see my issue. I am attaching again the 2 images.
IMAGE 1

IMAGE 2

Please let me know if I can provide any further information.

Thank you in advance.

1 Like