cancel
Showing results for 
Search instead for 
Did you mean: 

CAP (Node.js) - Cannot read properties of undefined (reading 'ID')

FedericoBelotti
Discoverer
0 Kudos
1,338

Good evening,
I'm developing a Node.js CAP application that exposes a service (TicketsService) composed of two entities:

  1. Tickets: main entity, projection of a remote entity.
  2. Reports: local entity, associated to Tickets where Tickets:Reports relation is 1:*.

Reports uses the cuid aspect, so the ID field is auto-generated when a new Report is created.

Without implementing custom handlers, I can succesfully execute CRUD operations on the Reports entity by taking advantage of the generic providers served by CAP.

I then implemented the TicketsService custom handlers to manage Tickets readings (which work fine), as well as the READ handler on the Reports entity.

Then, it was time for the CREATE handler, that I simply defined as follows:

this.on("CREATE", Reports, async (req, next) => {
    return await cds.run(req.query);
})<br>

since I don't have to manage custom logic for the Reports creation.

The problem occurs when I try to send a POST request to create a new Report:

[cds] - ️Uncaught TypeError: Cannot read properties of undefined (reading 'ID')
    at UriHelper.buildEntityKeys (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/utils/UriHelper.js:85:22)
    at ResponseHeaderSetter.setLocationHeader (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/core/ResponseHeaderSetter.js:102:17)
    at SetResponseHeadersCommand.execute (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/SetResponseHeadersCommand.js:66:30)
    at CommandExecutor._execute (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:71:17)
    at /home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:81:18
    at ConditionalRequestControlCommand.execute (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/ConditionalRequestControlCommand.js:48:5)
    at CommandExecutor._execute (/home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:71:17)
    at /home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/CommandExecutor.js:81:18
    at /home/user/projects/reg-reports/node_modules/@sap/cds/libx/_runtime/cds-services/adapter/odata-v4/okra/odata-server/invocation/DispatcherCommand.js:88:11
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {<br>

I then refactored the CREATE handler delegating the operation to the generic providers (or at least that's what I thought since I read that generic providers are executed after all custom ones):

this.on("CREATE", Reports, async (req, next) => {
    return next();
})<br>

But the error is the same.

Here are my doubts:

  1. Is there a way to only create custom handlers for certain entities (like remote ones) and still let the generic providers handle CRUD operations for the local entities?
  2. If the creation of a custom provider implies that I have to manage also all the other handlers for the remaining entities, how should I implement the on CREATE handler?

Thank you in advance for the help.
Federico

Accepted Solutions (1)

Accepted Solutions (1)

FedericoBelotti
Discoverer
0 Kudos

I managed to solve the issue. The problem was the instruction:

return await super.init();

not being called in the on.init() method inside the handler.

Answers (0)