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

CUID causes error with custom event handler on CAPM (NodeJS)

Afsal
Participant
0 Likes
1,182

Hi,
I am trying to implement a sample application with custom implementation logic.

schema.cds looks like

 using {
managed,
cuid,
User,
sap.common.CodeList
} from '@sap/cds/common';
namespace afs.csupport;
....
entity Users : cuid, managed {
firstName: String;
lastName: String;
email: EMailAddress @assert.format : '[\w.]+@[\w.]+';
phone: PhoneNumber;
_tickets: Association to many Tickets on _tickets._author = $self;
_assigned: Association to many Tickets on _assigned._assignedTo = $self;
}
.....

myservice.cds

using {afs.csupport as my} from '../db/schema';
service CustomerService @(path:'/service'){
......
entity users as projection on my.Users;
}


so far, POST, GET , DELETE operations works fine when tested in with postman.

but, when I added the myservice.js for testing custom implementation and event handling, the POST request failed and I got the following error in terminal

[odata] - POST /service/users 
[cds] - TypeError: Cannot read properties of undefined (reading 'ID')
at UriHelper.buildEntityKeys (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\utils\UriHelper.js:85:22)
at ResponseHeaderSetter.setLocationHeader (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\core\ResponseHeaderSetter.js:102:17)
at SetResponseHeadersCommand.execute (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\SetResponseHeadersCommand.js:66:30)
at CommandExecutor._execute (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\CommandExecutor.js:71:17)
at C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\CommandExecutor.js:81:18
at ConditionalRequestControlCommand.execute (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\ConditionalRequestControlCommand.js:48:5)
at CommandExecutor._execute (C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\CommandExecutor.js:71:17)
at C:\SAPDevelop\csupport\node_modules\@sap\cds\libx\_runtime\cds-services\adapter\odata-v4\okra\odata-server\invocation\CommandExecutor.js:81:18
at C:\SAPDevelop\csupport\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) {
id: '1591466',
level: 'ERROR',
timestamp: 1701164783226
}

myservice.js

const cds = require("@sap/cds/lib");
module.exports = class CustomerService extends cds.ApplicationService {
init() {
this.on("CREATE", "ticket", (req, next) => {
const data = req.data;
if (data.title.length < 10) return req.reject("The title is too short");
else return next();
});
this.on("READ", "ticket", (req, next) => {
return next();
});
this.on("CREATE", "users", (req, next) => {
return next();
});
}
};



Since the ID field is a cuid that is autogenerated, I am not passing that to the request.
Did I miss anything in the implementation file that supports the database level auto generated field.

here is the payload

{
"firstName":"Alice",
"lastName":"Warner",
"email":"alice@abc.com",
"phone":"9999548765"
}

Accepted Solutions (1)

Accepted Solutions (1)

david_kunz2
Product and Topic Expert
Product and Topic Expert

Hi afsal_a ,

At the end of your `init` method, please add `return super.init()`.

Best regards,
David

Afsal
Participant
0 Likes

Thanks david.kunz2 ,
That worked.

Answers (0)