Return a node js response inside session.withTransaction

Hi,

I am using session.withTransaction() to execute multiple updates in the mongo db. Please note that promiseArray has multiple Stock.update statemeng to update stock quantities.

 await session.withTransaction(
              async () => {
                promiseResults = await Promise.all(promiseArray);

                for (const result of promiseResults) {
                  recordCounter++;
                  if (result.nModified === 1) {
                    stockItemsNoUpdate.push(goodReturnSummary[recordCounter]);
                  }
                }

                if (stockItemsNoUpdate.length > 0) {
                  return res.status(200).send(response);
                }

                existingGoodReturnSummary = GoodReturn.build({
                  _id: sheetId,
                  goodReturnSummary,
                  agency,
                  createdBy,
                });
                await existingGoodReturnSummary.save({ session: session });

                existingGoodReturnSummary = await GoodReturn.calculateTotalGoodReturnAmount(
                  existingGoodReturnSummary,
                  session
                );
              },
              {
                readPreference: 'primary',
                readConcern: { level: 'local' },
                writeConcern: { w: 'majority' },
              }
            );

If stockItemsNoUpdate.length > 0 I need to abort this transaction and send the response. done by below code segment.

if (stockItemsNoUpdate.length > 0) {
  return res.status(200).send(response);
}

But I cannot do this because of the below error

Any idea on how to resolve this ??

Cheers