on 01-15-2022 9:16 AM
Hi experts,
I am using a view with parameters in my CAP project that is intended to be exposed to and used by other CAP services.
The documentation describes how to call these views via service URL like this: (which works just fine)
GET: /OrderWithParameter(foo=5)/Set or GET: /OrderWithParameter(5)/Set
GET: /ViewInService(p1=5, p2=true)/Set
I would like to know what the proper way would be to pass the parameters when these views are consumed programmatically from a different CAP project, e.g.:
// Consuming Service
const ext = await cds.connect.to('external_service')
const { ViewInService } = ext.entities
const results = await srv.run(SELECT.from(ViewInService)) // How to properly pass p1 and p2 to this query?
When I log the query of a View with parameter (e.g. when called via the browser) I can see that the passed parameters are part of the args object. Of course the consumer could just manually edit the query object, however I was hoping that there is better way, similar to how I would fluently pass where arguments to a query.
Thanks in advance,
Selim
Hi selimyavuz ,
The following works:
> SELECT.from('ViewInService(p1:5,p2:true)')
{ SELECT: {
from: {
ref: [
{
id: 'ViewInService',
args: { p1: {val:5}, p2: {val:true} }
}
]
}
}}
Best regards,
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks David! Works like a charm 🙂
Although I had to seperate the query from the actual request:
const reusults = await SELECT.from('ViewInService(p1:5,p2:true)') --> Error: invalid name of function or procedure...
const query = SELECT.from('ViewInService(p1:5,p2:true)')
const results = await srv.run(query) --> Success<br>
Thanks and best regards,
Selim
Hi selimyavuz
Can you please share the whole code snippet. I am trying to call external service from my cap project.
Declared as below
"entity Master(key: String(6)) as projection on ext.MasterData where key = :key" in master.cds file in my cap project.
Calling srv.run(SELECT.from`Master(key:'123')` ) in the master.js file. But it is not working for me. It is laking the url as /extservice/?$select=key. Where as I want it to be /extservice/MasterData(key=123)/Set.
Kindly help.
Thanks
Kanika
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.