# Undefined variable

**URL:** <https://community.postman.com/t/undefined-variable/38844>\
**Category:** Help Hub\
**Created:** [September 5, 2022, 9:16am UTC](https://community.postman.com/t/undefined-variable/38844 "2022-09-05T09:16:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 5, 2022, 9:16am UTC](https://community.postman.com/t/undefined-variable/38844/1 "2022-09-05T09:16:27Z")

</div>

I have a collection variable called artistId  
i wanted to check whether in the response body the value of artistId matches the value of that variable  
i think i did something wrong

i tried both of these:-

const response = pm.response.json();

pm.test(“Existing Artist Has Been Modified”, function(){  
var artistId = pm.collectionVariables.get(“artistId”)  
pm.expect.response.artistId.to.eql(artistId);

})

pm.test(“Existing Artist Has Been Modified”, function(){  
pm.expect.response.artistId.to.eql(pm.collectionVariables.get(“artistId”))  
})

and both gave me the same message:-  
TypeError: Cannot read properties of undefined (reading ‘artistId’)

---

<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:** [September 5, 2022, 1:26pm UTC](https://community.postman.com/t/undefined-variable/38844/2 "2022-09-05T13:26:43Z")

</div>

Calling variables the same name isn’t something that I would recommend as it can get messy with scope.

Call the stored artistID something like expectedArtistId

The define the responseArtistId something like…

```auto
var responseArtistId = response.ArtistId;

```

The test then comes a lot easier to work with.

```auto
pm.test(“Existing Artist Has Been Modified”, function(){
      pm.expect(response.ArtistId).to.eql(expectedArtistId); 
})

```

On face value though, your main problems is with this line.

```auto
pm.expect.response.artistId.to.eql(pm.collectionVariables.get(“artistId”))

```

Which should be…

```auto
pm.expect(response.artistId).to.eql(pm.collectionVariables.get(“artistId”))

```

---

<div class="post-metadata">

**Author:** ![hannahbroch](https://avatars.discourse-cdn.com/v4/letter/h/ecae2f/32.png) [@hannahbroch](https://community.postman.com/u/hannahbroch)\
**Post date:** [September 5, 2022, 3:52pm UTC](https://community.postman.com/t/undefined-variable/38844/3 "2022-09-05T15:52:03Z")

</div>

thank u so much  
wking gr8 now

---

<div class="post-metadata">

**Author:** ![sreekanth.t.31](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sreekanth.t.31](https://community.postman.com/u/sreekanth.t.31)\
**Post date:** [June 27, 2023, 1:32pm UTC](https://community.postman.com/t/undefined-variable/38844/4 "2023-06-27T13:32:12Z")

</div>

My Postman Test script required printing the length of the “clientID” variable. I tried the below Test script but it returns “Undefined”.

```auto
var responseJson = pm.response.json();
var cld = console.log((responseJson.content.outputVariables.oddOutput.clientId).length);
pm.test("ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is " +cld, () => {
    pm.expect(statText).to.eql(expectedStatText); 
)}

ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is undefined

```

---

<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:** [June 27, 2023, 2:17pm UTC](https://community.postman.com/t/undefined-variable/38844/5 "2023-06-27T14:17:25Z")

</div>

@sreekanth.t.31

This should have been its own question.

Remove the console log from the definition of the “cld” variable and then console log it separately afterwards.

```auto
var cld = responseJson.content.outputVariables.oddOutput.clientId.length;
console.log(cld);

```

If that doesn’t work, you will need to paste an example response, so we can see the structure.

On another note, I can’t see where the following variables are initialized in your example code.

```auto
pm.expect(statText).to.eql(expectedStatText); 

```

---

<div class="post-metadata">

**Author:** ![sreekanth.t.31](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sreekanth.t.31](https://community.postman.com/u/sreekanth.t.31)\
**Post date:** [June 27, 2023, 11:38pm UTC](https://community.postman.com/t/undefined-variable/38844/6 "2023-06-27T23:38:18Z")

</div>

Thank you @michaelderekjones

Below is my complete request and response

Test script:

```auto
var responseJson = pm.response.json();

var expectedStatText = "Unsuccessful";
var statText = responseJson.content.outputVariables.oddOutput.responseMessageList.transactionStatusText;
var cld = responseJson.content.outputVariables.oddOutput.clientId.length;

pm.test("ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is " +console.log(cld), () => {
    pm.expect(statText).to.eql(expectedStatText); 
	
})

```

Test Result

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/d/4d0af43989ffc63abf08abdd30ac9207748bf0a1.png)

I am expecting Test Result as  
“ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is 21”

---

<div class="post-metadata">

**Author:** ![danny-dainton](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/danny-dainton/32/29401_2.png) [@danny-dainton](https://community.postman.com/u/danny-dainton)\
**Post date:** [June 28, 2023, 7:54am UTC](https://community.postman.com/t/undefined-variable/38844/7 "2023-06-28T07:54:47Z")

</div>

Could you share the structure of the response, if the reference to that value is wrong, it would show as undefined.

---

<div class="post-metadata">

**Author:** ![sreekanth.t.31](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sreekanth.t.31](https://community.postman.com/u/sreekanth.t.31)\
**Post date:** [June 28, 2023, 8:56am UTC](https://community.postman.com/t/undefined-variable/38844/8 "2023-06-28T08:56:34Z")

</div>

```auto
{
    "transactionId": "xxxxxx",
    "content": {
        "outputVariables": {
            "oddOutput": {
                "apiVersion": "xxxx",
                "subClientId": "xxxxxxx",
                "clientId": "xxxxxxxxx"
                }
            }
        },
        "status": "xxxxxxxxxx",
        "startTimestamp": "xxxxxxxxxxxx",
        "endTimestamp": "xxxxxxxxxx",
        "timeTaken": 365
    }

```

---

<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:** [June 28, 2023, 9:18am UTC](https://community.postman.com/t/undefined-variable/38844/9 "2023-06-28T09:18:54Z")

</div>

Ok, dealing with your initial issue.

You can’t use console log in the test case name like that.

Use backticks and enclose the variable in ${} like the following.

```auto
pm.test(`ClientID. Expected: Length must 1-20 charaters, Actual: ClientID length is ${cld}`, () => {
    pm.expect(statText).to.eql(expectedStatText); 
})

```

This is using a concept called Template Literals and in particular string interpolation.

[Template literals (Template strings) - JavaScript | MDN (mozilla.org)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals)

I use it all the time to customize test case names, in particular if you are looping through a response.

However, I’m still not understanding your test.

Your example response does not include the transactionStatusText so surely this will be “undefined” and fail the test.

```auto
responseJson.content.outputVariables.oddOutput.responseMessageList.transactionStatusText;

```

The test itself is called “length must be 1-20 characters”, but you are not actually testing this but that the transactionStatusText = “unsuccessful”.

---

<div class="post-metadata">

**Author:** ![sreekanth.t.31](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@sreekanth.t.31](https://community.postman.com/u/sreekanth.t.31)\
**Post date:** [June 28, 2023, 9:37am UTC](https://community.postman.com/t/undefined-variable/38844/10 "2023-06-28T09:37:37Z")

</div>

Thank you, It worked.  
Sorry while posting the full response I deleted a few lines.

---

<div class="post-metadata">

**Author:** ![avionics-observer-25](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@avionics-observer-25](https://community.postman.com/u/avionics-observer-25)\
**Post date:** [July 2, 2023, 6:02pm UTC](https://community.postman.com/t/undefined-variable/38844/11 "2023-07-02T18:02:16Z")

</div>

I need vocalbilary test because I an a visual artist

---

<div class="post-metadata">

**Author:** ![avionics-observer-25](https://avatars.discourse-cdn.com/v4/letter/a/9fc29f/32.png) [@avionics-observer-25](https://community.postman.com/u/avionics-observer-25)\
**Post date:** [July 2, 2023, 8:54pm UTC](https://community.postman.com/t/undefined-variable/38844/12 "2023-07-02T20:54:04Z")

</div>

My test is the count of each photo download form printing platform on each photo at [gmail.com](http://gmail.com) phone canma
