# TypeError: Cannot read properties of undefined (reading 'map

**URL:** <https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754>\
**Category:** Help Hub\
**Tags:** tests, scripts, package-library\
**Created:** [May 7, 2024, 10:48am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754 "2024-05-07T10:48:33Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 10:48am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/1 "2024-05-07T10:48:33Z")

</div>

Created a package with function and invoked from request. But getting error

**Package:**

```auto
function extractTable(variableName) {
    var Itemdata = responseBody.map(item => item[variableName]);
    pm.environment.set(variableName, JSON.stringify(Itemdata));
}
module.exports = { extractTable }

```

**Script:**

```auto
const db = pm.require('@zz-xxxx-yy/processcollectionvariable');
db.extractTable('itetest');

```

**Error :**

```auto
Couldn't evaluate the test script:
TypeError: Cannot read properties of undefined (reading 'map')

```

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

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 10:58am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/2 "2024-05-07T10:58:29Z")

</div>

Hi @payload-geoscienti29.

Can you share the full code snippet? Where was responseBody defined?

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 11:32am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/3 "2024-05-07T11:32:56Z")

</div>

Apologies It worked. responseBody was defined in wrong place. Thank you!

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 11:46am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/4 "2024-05-07T11:46:15Z")

</div>

Could you please help me on this

**Script** :

```auto
const myPackage = pm.require('@xx-yy-zz/databasetables');
myPackage.processArrayVariable('item_supplier_id');

```

**Package script:**

```auto
function processArrayVariable(columnName) {
    const colValue = pm.environment.get(columnName);
    if (colValue) {
        const colArray = colValue.replace(/[\[\]]/g, '').split(',');

        colArray.forEach((colItem, index) => {
            pm.environment.set(`${columnName}${index + 1}`, colItem);
        });
    } else {
        console.log(`${columnName} is not defined in the environment.`);
    }
}
module.exports = { processArrayVariable }

```

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

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 11:52am UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/5 "2024-05-07T11:52:17Z")

</div>

Hi @payload-geoscienti29. I’m not very certain of what the issue could be here, but check that you’re importing the right package and that the package has been saved.

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:00pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/6 "2024-05-07T12:00:18Z")

</div>

Yes the package was correct and function name is auto populated  
 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/4/9/49eea92770b90702d1130d5264582093b94b23a7.png)

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 12:01pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/7 "2024-05-07T12:01:59Z")

</div>

Okay. What version of Postman are you on? (Settings \> About)

Can you also share what OS & version you’re using?

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:04pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/8 "2024-05-07T12:04:48Z")

</div>

Postman version :11.0.7  
Windows 11 Pro

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 12:07pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/9 "2024-05-07T12:07:24Z")

</div>

Hhmm. You’re on the latest version.

The previous package you were working on resolved correctly, right? Just this one isn’t resolving?

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:08pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/10 "2024-05-07T12:08:43Z")

</div>

yes. getting this error for another function within the package

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 12:11pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/11 "2024-05-07T12:11:12Z")

</div>

To be sure I understand you correctly. It’s the same package with two functions; one function works, but the other doesn’t. Right?

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:13pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/12 "2024-05-07T12:13:50Z")

</div>

Exactly. Not sure what was missed

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 12:20pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/13 "2024-05-07T12:20:28Z")

</div>

Can you share the full package code and how you’re using the package functions with me? Feel free to abstract implementation logic. Something like

```auto
function processArrayVariable(columnName) {
        // Implementation goes here
}

function processSecondFunction(columnName) {
        // Implementation goes here
}
module.exports = { processArrayVariable, processSecondFunction }

```

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:31pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/14 "2024-05-07T12:31:39Z")

</div>

I added module.exports individually that caused an issue.  
**Issue:**  
function processArrayVariable(columnName) {  
// Implementation goes here  
}  
module.exports = { processArrayVariable}  
function processSecondFunction(columnName) {  
// Implementation goes here  
}  
module.exports = { processSecondFunction }

**Solution:**  
This worked  
module.exports = { processArrayVariable, processSecondFunction }

---

<div class="post-metadata">

**Author:** ![gbadebo-bello](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/gbadebo-bello/32/12175_2.png) [@gbadebo-bello](https://community.postman.com/u/gbadebo-bello)\
**Post date:** [May 7, 2024, 12:33pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/15 "2024-05-07T12:33:58Z")

</div>

Ohh. I see.

`module.exports` can only be used to export one single object. You can nest multiple functions in that object.

---

<div class="post-metadata">

**Author:** ![payload-geoscienti29](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@payload-geoscienti29](https://community.postman.com/u/payload-geoscienti29)\
**Post date:** [May 7, 2024, 12:39pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/16 "2024-05-07T12:39:47Z")

</div>

Thank you so much for your support!

---

<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:** [May 10, 2024, 12:40pm UTC](https://community.postman.com/t/typeerror-cannot-read-properties-of-undefined-reading-map/60754/17 "2024-05-10T12:40:35Z")

</div>

Closed on May 10, 2024, 12:40pm UTC

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