Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read with an after hook implementation fails from Fiori Elements #32

Open
rpanneel opened this issue Jan 19, 2020 · 0 comments
Open

Read with an after hook implementation fails from Fiori Elements #32

rpanneel opened this issue Jan 19, 2020 · 0 comments

Comments

@rpanneel
Copy link

Hello CAP Enthusiasts,

During my local testing I ran into a strange issue using the Fiori Elements front-end.

I've added a virtual spotsAvalaible property to my entity Courses. This virtual property I'm going to fill in the after hook of the READ of the Courses:

srv.after('READ', 'Courses', (courses, req) => {
            // problem from fiori: couses has counted object --> $count
            return courses
                .map(async course => {
                    // Get the reservations for this course
                    const reservations = await cds.transaction(req).run(
                        SELECT.from(Reservations).where({course_ID: course.ID})
                    )
                    // Caluclate spots taken
                    const spotsTaken = reservations.reduce((total, reservation) => total + reservation.quantity, 0)
                    course.spotsAvailable = course.spots - spotsTaken
                })
        })

This works when I use postman and do a get on the courses. (http://localhost:4004/training/Courses)

{
"@odata.context": "$metadata#Courses",
"@odata.metadataEtag": "W/\"BmT9sdj4fnWqz4BxxDN0o+XSNGD5CW69Y8wbhe1uRYk=\"",
"value": [
{
"ID": "ee0b3d38-729d-4237-a7fe-b922f753e87e",
"modifiedAt": null,
"createdAt": "2020-01-18T18:45:57Z",
"createdBy": "anonymous",
"modifiedBy": null,
"title": "Some course",
"days": 1,
"spots": 15,
"courseType": "Technical",
"spotsAvailable": 15,
"trainer_ID": "880331ae-ae41-4a46-b49b-b7a3cbd049b0"
}
]
}

But when using the Fiori elements front-end the hook crashes with following error. For the following request in the $batch:
GET Courses?$count=true&$select=ID,days,spots,title,trainer_ID&$expand=trainer($select=ID,name)&$skip=0&$top=30 HTTP/1.1

`
POST /training/$batch

READ Courses {
'$count': 'true',
'$select': 'ID,days,spots,title,trainer_ID',
'$expand': 'trainer($select=ID,name)',
'$skip': '0',
'$top': '30'
}
READ Courses {
'$count': 'true',
'$select': 'ID,days,spots,title,trainer_ID',
'$expand': 'trainer($select=ID,name)',
'$skip': '0',
'$top': '30'
}
[2020-01-19T14:45:18.101Z | ERROR | 1221510]: Cannot read property 'run' of undefined
TypeError: Cannot read property 'run' of undefined
at C:\repo\CAP\training-reservations\node_modules@sap\cds-services\lib\connect\Transaction.js:84:27
`

When debugging i notice that the after-hook gets called a second time, but this time the courses-parameter is filled with a counted object:

[{counted: 1}]

Is there a way to solve this?

Thanks!

Kind regards,

Robin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant