on ‎2023 Nov 28 10:02 AM
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"
}
Request clarification before answering.
Hi afsal_a ,
At the end of your `init` method, please add `return super.init()`.
Best regards,
David
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 13 | |
| 8 | |
| 7 | |
| 5 | |
| 4 | |
| 3 | |
| 2 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.