I want past pre-request from folder in pre-request from request in Postman?

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

My question:

I have a script from pre-request folder I want past this script automatically in all my request start whit P
Details (like screenshots):

This is my script in pre-request in my Collection:
const folderName = ‘BO_GetCardDetails_35’;

// Get the current collection
const collection = pm.collection;
// Iterate through the collection items
collection.items.each((item) => {
// Check if the item belongs to the specified folder and starts with “P”
if (item.folder && item.folder.name === folderName && item.request.method === ‘POST’ && item.name.startsWith(‘P’)) {
// Add the pre-request script
listen: ‘prerequest’,
script: {
type: ‘text/javascript’,
exec: var testData = pm.variables.get('pm-test-data'); let testDataArray = JSON.parse(testData); let currentTestName = pm.info.requestName; for (let i = 0; i < testDataArray.length; i++) { if (testDataArray[i].testcase_namme == currentTestName) { pm.variables.set('valid-card-number-debit-st-n', testDataArray[i].cardnumber); break; } } let requestBody = pm.request.body.raw; let updatedBody = requestBody.replace(/(<CardNumber>)(<\/CardNumber>)/, '$1' + pm.variables.get('valid-card-number-debit-st-n') + '$2'); pm.request.body.raw = updatedBody; ,

console.log(‘Pre-request scripts added for requests starting with “P” in the folder "’ + folderName + ‘".’);

And I want this script past automatically in my pre-request from all my request start with P , In my test result show me : TypeError: Cannot read properties of undefined (reading ‘items’)

How I found the problem:

I’ve already tried: