TypeError: Cannot read properties of undefined (reading 'map

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

Package:

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

Script:

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

Error :

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

Hi @payload-geoscienti29.

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

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

Could you please help me on this

Script:

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

Package script:

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 }

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.

Yes the package was correct and function name is auto populated
image

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

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

Postman version :11.0.7
Windows 11 Pro

Hhmm. You’re on the latest version.

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

yes. getting this error for another function within the package

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

Exactly. Not sure what was missed

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

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

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

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 }

Ohh. I see.

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

1 Like

Thank you so much for your support!

1 Like

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