cancel
Showing results for 
Search instead for 
Did you mean: 

SAP CDS: Post to remote service "Cannot read properties of undefined (reading 'length')"

miqweias
Explorer
0 Kudos

I have this function in a NodeJS application using @sap/cds version 7.3.0:

static async getEquipmentSet() {
        try {
            const api = await cds.connect.to('sap-odata-service');
            const result = await api.get(`EquipmentSet`);
            return result;
        } catch (error) {
            console.log(error);
        }
}<br>

This previous GET function returned data from the remote service 'sap-odata-service' configured in the .cdsrc.json file.

But the following POST method returns an error:

static async postHourmeter(equipmentData, measurementPointId) {
        try {
            const payload = this.getHourmeterPayload(equipmentData, measurementPointId);
            const api = await cds.connect.to('sap-odata-service');
            const result = await api.post(`MeasureDocumentSet`, payload);
            return result;
        } catch (error) {
            console.log(`The error is:`);
            console.log(error);
        }
    }<br>

Errors:

   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT TypeError: Cannot read properties of undefined (reading 'length')
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at findQueryTarget (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/common/utils/resolveView.js:766:51)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at RemoteService.handle (/home/vcap/app/node_modules/@sap/cds/libx/_runtime/remote/Service.js:321:17)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at RemoteService.dispatch (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:45:15)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:18:43
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at /home/vcap/app/node_modules/@sap/cds/lib/srv/srv-tx.js:38:55
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at AsyncLocalStorage.run (node:async_hooks:338:14)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at AsyncLocalStorage.run (/home/vcap/app/node_modules/@sap/cds/lib/req/cds-context.js:7:36)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at RemoteService.srv_tx (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-tx.js:38:25)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at RemoteService.dispatch (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-dispatch.js:18:31)
   2023-10-23T15:00:06.11+0000 [APP/PROC/WEB/0] OUT at RemoteService.run (/home/vcap/app/node_modules/@sap/cds/lib/srv/srv-api.js:77:17)<br>

Does anyone know how to fix it?

martinstenzig
Contributor
0 Kudos

Could you provide more details about what SAP API you are actually trying to call. Just because you can read one entity ('EquipmentSet') does not mean you can write another entity ('MeasureDocumentSet').

I would start with a REST call (POST) outside of the code against the MeasureDocumentSet to if that works.

miqweias
Explorer
0 Kudos

I put a try/catch statement in node_modules/@sap/cds/libx/_runtime/common/utils/resolveView.js:766:51 and the application runs. The following snippet causes the error:

return q.SELECT && q.SELECT._transitions ? q.SELECT._transitions[q.SELECT._transitions.length - 1].target : q.INSERT ? q.INSERT._transitions[q.INSERT._transitions.length - 1].target : q.UPDATE ? q.UPDATE._transitions[q.UPDATE._transitions.length - 1].target : q.UPSERT ? q.UPSERT._transitions[q.UPSERT._transitions.length - 1].target : q.DELETE ? q.DELETE._transitions[q.DELETE._transitions.length - 1].target : undefined

Specially in : q.INSERT._transitions[q.INSERT._transitions.length - 1].target.

The application is running, but the problem now is that we need to install the dependencies from npm...

Willem_Pardaens
Product and Topic Expert
Product and Topic Expert
0 Kudos

As Martin mentioned, you'll need to provide more information about the remote source you're connecting to. Which API is it, and how is it defined in your package.json?

Accepted Solutions (0)

Answers (1)

Answers (1)

cristiansolisc
Discoverer
0 Kudos

Hi olena_!

Do you have more information about the solution with respect this issue we are facing in SAP CAP?

"Cannot read properties of undefined (reading 'length')"

Thanks in advance