2024 Oct 09 1:13 PM - edited 2024 Oct 10 7:37 AM
In my CAP backend project I use nestjs. In version 7.9.3 of the @sap/cds and @sap/cds-dk packages, everything worked as expected. After updating these packages to version 8, an error occurs when I call my cds service: “ ERROR TypeError: Cannot read properties of undefined (reading ‘definitions’)”. The error occurs both in the $metadata and during a read operation on an entity.
The code snippet where I serve the OData service using @sap/cds framework:
async configure(_consumer: MiddlewareConsumer) {
await cds
.serve('SampleService')
.in(this.adapterHost.httpAdapter)
.from("cds")
.at('/odata')
.with(this.handler.serviceHandler);
}
You can find my project on GitHub: https://github.com/BastiInnovation/cap-template
Request clarification before answering.
The solution provided via SAP Me:
When using a custom server file (what we do here), the cds.model has to be set manually. This can either be achieved via:
const csn = await cds.load('*') // or 'cds' in our case
.then (cds.minify)
cds.model = cds.compile.for.nodejs (csn)
or by precompiling the model at build time via an npm script and loading its resulting JSON into cds.model at application startup:
// package.json > scripts
"prebuild": "cds compile cds -4 nodejs -o src/model.json"
// App initialization:
cds.model = require ('./model.json');
The former solution can also meanwhile be found within the CAPire documentation at https://cap.cloud.sap/docs/node.js/cds-server#built-in-server-js .
Since @cap-js/cds-types did not support `cds.compile.for.nodejs` and `cds.minify` at the time of issue creation, but in the meantime the respective issue (https://github.com/cap-js/cds-types/issues/292) has been closed and should be available pretty soon.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @BastiMankel ,
first of all, when working with @sap/cds@8.3.1 and @sap/cds-dk@8.3.0 everything works except for the $metadata request.
After debugging through your sample code with @hose versions I saw, that the metadata error is cause by the metadata middleware (@sap/cds/libx/odata/middleware/metadata.js => line 66). This relies on having either cds.context.model or cds.model being properly filled, which is not the case.
I couldn't find out, why both of which are undefined, especially since the `service` variable shows, that the model is properly loaded.
Tweeking line 66 of the metadata.js from
const csnService = (cds.context.model || cds.model).definitions[service.definition.name]
to
const csnService = (cds.context.model || cds.model || service.model).definitions[service.definition.name]
makes the metadata work again, but that is something, that needs to be addressed to the SAP team working on @sap/cds or SAP Cloud SDK, I think.
When I tried comparing the differences in that metadata middleware between @sap/cds@7.9.3 and @sap/cds@8.3.1, I noticed, that middleware is not activated in version 7.9.3 but is in 8.3.1 without having done anything within your project setup. Unfortunately, I did not find any hint on that behaviour change in the CAPire docs.
Looking forward to seeing any progress on that topic since at the moment it's blocking our migration to CAPv8...
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi sorry!
Yes, I have now pushed a version where the error you described no longer occurs.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
41 | |
15 | |
10 | |
9 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.