Search code examples
json-server

JSON-Server - sort returns empty array


I am running mock API on json-server and able to fetch data in URL with URL but when I pass /products?_sort=price&_order=asc I receive empty array.

I am able to fetch all products data through localhost:8080/products but when I add _sort and _order as per JSON server documents.

Not able to figure out the problem why it is returning empty array.

Json server docs
Sort :Add _sort and _order (ascending order by default)

GET /posts?_sort=views&_order=asc


Solution

  • JSON Server's behavior for sorting changed with different versions.

    To resolve the issue, update JSON Server using the command:

    npm install -g json-server

    After updating, sorting by default will be ascending. No need to use _order:asc

    GET /products?_sort=price

    For descending order,

    use GET /products?_sort=-price

    This will definitely help.

    Best Of Luck ;)

    Noor.