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

CAP - How to programmatically order by a certain column when using a path expression in NodeJS?

rui_bessa
Discoverer
0 Likes
2,273

Hello experts,
I've been trying different ways of ordering by a certain column on a path expression while using CAP v7 with NodeJS and I have not been successful yet.

I've searched through the documentation but unfortunately was not able to find a similar case to mine.
Given the following example and supposing that the authors projection has a column named age : 

 

let books = await SELECT.from('Books').columns(books => {
        books `*`,
        books.authors('*')
    }).where({ID : 1});

 

 How can I order all the authors from the book with ID = 1 by age ?

I've tried the following:

 

let books = await SELECT.from('Books').columns(books => {
        books `*`,
        books.authors('*').orderBy('age desc')
    }).where({ID : 1});

 

But this leads to the following error : 
[cds] - TypeError: Cannot read properties of undefined (reading 'age')

Is there any possible way I can order authors by a column in this case?
Thanks!

View Entire Topic
Dinu
Active Contributor
0 Likes

This might be what you are looking for from syntax perspective:

 

let books = await SELECT.from('Books').columns(books => {
        books `*`,
        books.authors('*')`[order by age desc]`
    }).where({ID : 1});

 

This is extended from the syntax for infix filters. But, I'm not sure if that sorts the inner array as you are expecting. My guess is sorting is not yet supported.