Search code examples
node.jsknex.js

How to make sorting?


If look at the example in the documentation knex then sorting is done this way:

.orderBy('name', 'desc')

How to sort, if want the name to be sorted both in ascending order and in descending order, depending on what values ​​were specified in the route ?order=asc or ?order=desc.

I tried to do so .orderBy('name', req.orderBy), but it does not help.


Solution

  • .orderBy('name', req.query.order)
    

    Try like this