cancel
Showing results for 
Search instead for 
Did you mean: 

CAP: How to properly pass parameters to a View when consumed programmatically (Node.js)

selimyavuz
Explorer

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

selimyavuz
Explorer
0 Kudos

david.kunz2 christian.georgi gregorw any pointers / help would be much appreciated!

Accepted Solutions (1)

Accepted Solutions (1)

david_kunz2
Advisor
Advisor

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,
selimyavuz
Explorer

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

Answers (1)

Answers (1)

KM11
Participant
0 Kudos

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