cap-js / cds-dbs

Monorepo for SQL Database Services for CAP
https://cap.cloud.sap/docs/
Apache License 2.0
37 stars 11 forks source link

Sub entity creation issue #801

Closed C5295836 closed 2 months ago

C5295836 commented 2 months ago

"@cap-js/sqlite": "^1.5.0", "@sap/cds": "^7.9.0", "@sap/cds-dk": "^7.9.5", "sqlite3": "^5.0.4" node version v20.13.1

Error message: "TypeError: Cannot read properties of undefined (reading '$now')"

Data model:

entity Organizations : cuid, managed {
...
landscapeAssignments                : Composition of many LandscapeAssignments  on landscapeAssignments.organization = $self;
}

entity LandscapeAssignments : cuid, managed {
...
members                        : Composition of many LandscapeAssignmentMembers on members.landscapeAssignment = $self;
}

entity LandscapeAssignmentMembers : cuid, managed {
...
}

Code:

let landscapeAssignment = await service
        .post(LandscapeAssignments)
        .entries([
          {
           ...
            organization_ID: corePackageAssignment.organization_ID,
          },
        ]);

if (!landscapeAssignment) return; 

await service
          .post(LandscapeAssignmentMembers)
          .entries([
            {
              ...
              landscapeAssignment_ID: landscapeAssignment.ID,
            },
          ]);

Error "TypeError: Cannot read properties of undefined (reading '$now')" happens on await next(); of 'SAVE' handler: this.on( "SAVE", [LandscapeAssignmentMembers, LandscapeAssignmentMembers.drafts], async (req, next) => { ... await next(); } );

patricebender commented 2 months ago

which @cap-js/db-service version are you using? I can see your @cap-js/sqlite version is quite old. Please update all your dependencies. If the issue persists, provide a sample project (or your project) with detailed steps to reproduce your scenario.

C5295836 commented 2 months ago

We updated to latest version and issue is gone. Thanks!

C5295836 commented 2 months ago

hm. Error message is gone, however entity isn't created. Something still go wrong on "await next()", because next handler is not reached : this.after("SAVE", [LandscapeAssignmentMembers, LandscapeAssignmentMembers.drafts], async (laMember) => { ... } );

patricebender commented 2 months ago

Glad your initial problem is solved :)

I can't help you with your follow up problem though. Does not seem to be related to the database services. Please consult the documentation or report an internal issue in the cap/issues repository.