cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

No return for REST call using unbound function

0 Kudos
709

I am using an unbound function in my dtv-service.cds.

service DTVService { <br> function getProject(EXT_ID : String) returns array of { <br> PROJ_ID : String; <br> PROJ_STATUS : String; <br> };<br> ...<br>}

And in my dtv-service.js, I am calling a REST endpoint like this.

const cds = require("@sap/cds")
class DTVService extends cds.ApplicationService {
init() {
this.on("getProject", async (req) => {
const dtvapi = await cds.connect.to("dtvapi")
return dtvapi.get("/getstatus/BDT_PROJECT") }),
...
module.exports = { DTVService }

I am calling the endpoint from Postman like this.

http://localhost:4004/dtv/getProject(ext_id='BDT_PROJECT')

The result is returned "empty".

{"@odata.context":"$metadata#DTVService.return_DTVService_getProject"}

I am wondering why the value is not returned. When I go into the backend, I can see the request coming in, it is as well calculating the result correctly and returning it. When I call the endpoint directly from Postman (without CAP), I get the result like this.

[{"PROJ_ID":"BDT_PROJECT","PROJ_STATUS":"New"}]

Any idea why this happens? I have already tried to play around with how the REST endpoint is called in the service implementation and the definition of the service in CDS.

Accepted Solutions (1)

Accepted Solutions (1)

johannesvogel
Product and Topic Expert
Product and Topic Expert

Hi Stephan,

just by guessing:

You define a function that returns an object.

In the response for the REST, it's an array. Is it possible that your remote application returns an array and not a single object?

I think you either need to adapt the function return type or simply return the first element of the result of your request to your remote service.

The OData protocol adapter is picky on the return types. The REST adapter does not seem to validate that.

UPDATE

I was now able to fix the issue. In addition to the return type, which should be array, the parameters were in capital letters from the REST endpoint but in the service definition I used lowercase. After changing the service definition accordingly, the data is now returned.

___________

Thanks for the reply. I have changed the return type now to array. However, the result is still empty.

{"@odata.context":"$metadata#Collection(DTVService.return_DTVService_getProject)","value":[{}]}

If I run the query directly on the REST endpoint, the result looks as follows.

[{"PROJ_ID":"BDT_PROJECT","PROJ_STATUS":"New"}]

I guess the return type array is the correct option. However, via the CAP app I still do not get the result.

Answers (0)