Skip to Content
0
Nov 23, 2021 at 11:39 AM

CAPM: undefined is not iterable

145 Views Last edit Nov 23, 2021 at 01:05 PM 7 rev

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);
    }
  });
});