# Postman collection returns error when run in Postman CLI

**URL:** <https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421>\
**Category:** Help Hub\
**Tags:** tests, variables, response, postmancli\
**Created:** [March 4, 2024, 11:21am UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421 "2024-03-04T11:21:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![docking-module-par12](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@docking-module-par12](https://community.postman.com/u/docking-module-par12)\
**Post date:** [March 4, 2024, 11:21am UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421/1 "2024-03-04T11:21:06Z")

</div>

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](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](https://us1.discourse-cdn.com/getpostman/original/3X/a/f/aff04d54cdefaf6f9c37e7b5218a49ef78aee322.png)

CLI-

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/e/2e4fa386bd7d8b26294e2e6549a9ec04141489a0.png)

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [March 4, 2024, 5:28pm UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421/2 "2024-03-04T17:28:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![docking-module-par12](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@docking-module-par12](https://community.postman.com/u/docking-module-par12)\
**Post date:** [March 5, 2024, 5:17am UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421/3 "2024-03-05T05:17:12Z")

</div>

@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-

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/1/b/1b4fd864504645e9745b51eb2117c846c4e67bb3.png)

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

---

<div class="post-metadata">

**Author:** ![docking-module-par12](https://avatars.discourse-cdn.com/v4/letter/d/b3f665/32.png) [@docking-module-par12](https://community.postman.com/u/docking-module-par12)\
**Post date:** [March 5, 2024, 9:01am UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421/4 "2024-03-05T09:01:04Z")

</div>

@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 😑. So, there was restriction.

Now I can run whole collection smoothly 😇.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 4, 2024, 9:02am UTC](https://community.postman.com/t/postman-collection-returns-error-when-run-in-postman-cli/58421/5 "2024-04-04T09:02:00Z")

</div>

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