Postman collection returns error when run in Postman CLI

When sending my request manually it runs correctly and all the test is Pass.
But when I run the same with scheduled runs or CLI it shows error in the request-

□ End to End API Testing / Create story issue type in new project
└ Get all projects
GET https://aniketnawal.atlassian.net/rest/api/3/project/search [200 OK, 1.07kB, 402ms]




1⠄ TypeError in test-script

  1. TypeError Cannot read properties of undefined (reading ‘id’)
    at test-script
    inside “End to End API Testing / Create story issue type in new project / Get all
    projects”

As much I understood there is no problem with test-script as the it is executing correctly when sent manually.
But when running with Postman CLI, the response body is not being correctly returned or there is some problem in storing/parsing of it.

Can someone suggest a solution to this.

Thanks!!

This is my Test script-

const resp = pm.response.json();
const projectdetails = resp.values;
console.log(projectdetails);

const projectnames = projectdetails.map((projectname) => projectname.name);
console.log(projectnames);

const indexofsecondproject = projectnames.indexOf(‘SecondProject’);

pm.collectionVariables.set(‘SecondProjectID’, projectdetails[indexofsecondproject].id);

pm.collectionVariables.set(‘SecondProjectKey’, projectdetails[indexofsecondproject].key);

Console-
image

CLI-

First of all, you don’t have any tests in that script.

It’s just code in the tests tab. Semantics, but an important aspect.

Usually, you would parse the response, and then have an actual test checking for the response code or that the elements you want to set as collection variables actually exist before you set the collection variables.

This would give you a better understanding on what is failing. Rather than receiving a generic TypeError because it can’t find the element in the response.

It’s the setting of the collection variables that is erroring because it can’t find the ID element.

Do you not get to see the results of the console logs somewhere in the CLI? Or is that API just returning two blank arrays?

Is “indexofsecondproject” a variable, or is it just something your showing us here? Shouldn’t it be a 0 or 1?

Ideally want to console log the response and ensure those elements are being returned or wrap your code in tests. Either should provide you the feedback you need to troubleshoot where this is failing.

@michaelderekjones ,
Thanks for your prompt respond.

Firstly, you are correct there is no test in the code, it is a code for setting a collection variable from the response body.

The response when parsed through the method pm.response.json(), returns an
arrray JS objects/attributes, and when logged I can see the response body in console as well.

‘indexofsecondproject’ is a variable (basically its the index of “secondProject” in the project names array). I missed a line, now I have corrected that, you can recheck the code for clarification.

I can see the console logs in CLI but the API is returning blank array, though the same API is returning correct array(not blank) when request is sent manually.

From the Cloud- Run results-

This seems that response body is not available, I can use ‘run configuration’ option to persist response only for manually runs.

@michaelderekjones
Thank You for your support I was able to resolve the problem.
On logging the whole response, I observed that the response body was appearing incorrect as I was using global variable for auth token :expressionless:. So, there was restriction.

Now I can run whole collection smoothly :innocent:.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.