cancel
Showing results for 
Search instead for 
Did you mean: 

CAP: Custom logic not receiving query parameters

kammaje_cis
Active Contributor
0 Kudos

Hi,

I am using a query as below.

http://localhost:4004/analytics-output/ApplicationUsageByPeriod?$filter=timestamp gt 2018-09-04T00:55:54Z

I have a custom handler for this call. I wanted to check how query parameters appear, so I did something like this (just for testing).

module.exports = cds.service.impl (srv => {
 srv.on('READ','ApplicationUsageByPeriod', (req)=>{
 req.reject(409, JSON.stringify(req.data));
 // req.reject(409, JSON.stringify(req.query));
 })
});

But I got an empty output as below.

<error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
<code>409</code>
<message>{}</message>
</error>

But if I use req.query instead of req.data, I get the following output. This seems alright.

<error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
<code>409</code>
<message>
{"SELECT":{"from":{"ref":["AnalyticsOutput.ApplicationUsageByPeriod"]},"columns":[{"ref":["application"]},{"ref":["applicationName"]},{"ref":["count"]},{"ref":["timestamp"]}],"where":[{"ref":["timestamp"]},">",{"val":"2018-09-04T00:55:54Z"}],"limit":{"rows":{"val":1000}}}}
</message>
</error>

So I am unable to understand why I do not get output when I use req.data. Has anyone encountered this?

qmacro
Developer Advocate
Developer Advocate

Just wanted to say: nice question, well composed and easy to understand with useful code blocks, thanks!

Accepted Solutions (1)

Accepted Solutions (1)

vansyckel
Advisor
Advisor
0 Kudos

Hi Krishna,

req.data should not contain the query options. Actually, we currently do not add them to req object directly, but it is on the roadmap.

In the meantime, you can get them from the original express request object at req._.req.query.

Best,
Sebastian

kammaje_cis
Active Contributor
0 Kudos

Thanks, Sebastian. Can you please guide me to the roadmap document for CAP?. I searched but could not get it.

vansyckel
Advisor
Advisor
0 Kudos

Hi Krishna,

Sorry to say, but the roadmap is not available externally.

Best,
Sebastian

Answers (1)

Answers (1)

qmacro
Developer Advocate
Developer Advocate

I think req.data will give the data sent in the payload (i.e. body) of, for example, a POST request, rather than anything else. That to me, without even looking, is what "feels" right with that API name, from other libraries in the same space.

I note that the docu at https://cap.cloud.sap/docs/node.js/api#reqdata--- says "Captures all query parameters as well as http post bodies as a single object" but I'm not sure that's accurate, at least not in my experience - perhaps one of the team can comment here too.

I just tested this too, and got what I expected:

module.exports = srv => {
  srv.before('CREATE','Things', req => {
    console.log("DATA:", req.data)
    console.log("QUERY:", req.query)
  })
}

And with this request:

curl -H 'Content-Type: application/json' -d '{"ID": 1, "name":"DJ"}' http://localhost:4004/test/Things

I get this output:

DATA: { ID: 1, name: 'DJ' }
QUERY: { INSERT: { into: 'dj.Things', entries: [ { ID: 1, name: 'DJ' } ] } }

Cheers

kammaje_cis
Active Contributor
0 Kudos

Thanks, DJ for the confirmation. In this case, code seems to be catching up with the SAP documentation. 🙂

vansyckel
Advisor
Advisor

Hi DJ,

You are right that "Captures all query parameters as well as http post bodies as a single object" is erroneous. Thanks for pointing that out.

Best,
Sebastian