cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

CAPM: undefined is not iterable

EkanshCapgemini
Active Contributor
0 Likes
2,249

Hi,

I am following the learning journey 'Developing with SAP Extension Suite' from SAP BTP Learning Group. I am consuming Business Partner service from S4HANA Cloud via API Gateway sandbox. According to the learning journey, I am implementing my own service handler to handle $expand case when it calls data from business partners. However, I am getting error in cds watch logs that 'TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))'.

Here is my code

const cds = require("@sap/cds");

// service implementation with handlers
module.exports = cds.service.impl(async function () {
  const { Risks, BusinessPartners } = this.entities;

  // connect to remote service
  const BPSrv = await cds.connect.to("API_BUSINESS_PARTNER");

  // to handle $expand=bp call
  this.on("READ", Risks, async (req, next) => {
    try {
      const res = await next();
      if (Array.isArray(res)) {
        const aColumns = req.query.SELECT.columns || [];
        const aBpColumn = aColumns.find((column) => column.ref.includes("bp"));
        if (aBpColumn.expand && aBpColumn.expand.length > 0) {
          // $expand=bp was used
          const aRiskBps = res.map((risk) => risk.bp_BusinessPartner);
          const aBps = await BPSrv.run({
            query: SELECT.from(BusinessPartners)
              .where({ BusinessPartner: { in: aRiskBps } })
              .columns(["BusinessPartner", "FirstName", "LastName"]),
            headers: {
              apiKey: process.env.apiKey,
            },
          });
          console.log(aBps);
        }
      }
    } catch (error) {
      console.error(error);
    }
  });
});

Accepted Solutions (1)

Accepted Solutions (1)

johannesvogel
Product and Topic Expert
Product and Topic Expert
0 Likes

Hi ekansh005 ,

"BPSrv.run" is not designed to provide custom headers for a request to a remote service.

Did you see that snippet in the learning journey or in one of our samples?

Please either use .send in order to send custom headers with the request or consider adding the apiKey as a declarative header to the app defined destination.

Hope that helps,

Johannes

EkanshCapgemini
Active Contributor
0 Likes

Ahh... I misread .send as .run and was banging my head 🙂

Thanks for your explanation and it works as expected on changing it to .send().

BR, Ekansh

Answers (0)