Sorting at the server side is not happening

I am having trouble with server-side sorting using ORM Sequelize. By default, the sorting is supposed to be done by name in ascending order. However, when I try to change it to descending order on the client side, nothing happens despite seeing the request being displayed in the terminal.

Executing (default): SELECT count(*) AS "count" FROM "spr_turbodrills" AS "spr_turbodrills";

Executing (default): SELECT "turbodrill_id", "name", "spindle", "turbodrill_n", "createdAt", "updatedAt" FROM "spr_turbodrills" AS "spr_turbodrills" ORDER BY "spr_turbodrills"."name" ASC LIMIT 1 OFFSET 0;

It seems that the sorting based on the SQL query did not actually happen as intended.

Despite everything appearing fine on this end.

GET /api/directory/spr_turbodrills/all?order=desc&pageSize=1&page=1 200 7.805 ms - 163

Controller Method:

module.exports.getAllPaginate = async function (req, res) {
    try {
        const query = {
            offset:  +req.query.pageSize * ( +req.query.page - 1),
            limit:  +req.query.pageSize,
            order: [
                ['name', 'ASC']
            ]
        }
    
        const spr_turbodrills = await SprTurbodrills.findAndCountAll(query)
        res.status(200).json(spr_turbodrills)
    } catch(e) {
        errorHandler(res, e)
    }   
}

Answer №1

It seems like the issue may be related to your controller always using ASC as the default order.

module.exports.getAllPaginate = async function (req, res) {
try {
    const query = {
        offset:  +req.query.pageSize * ( +req.query.page - 1),
        limit:  +req.query.pageSize,
        order: [
            ['name', req.query.order]
        ]
    }

        const spr_turbodrills = await SprTurbodrills.findAndCountAll(query)
        res.status(200).json(spr_turbodrills)
    } catch(e) {
        errorHandler(res, e)
    }   
}

You might want to consider making a change by trying this:

GET /api/directory/spr_turbodrills/all?order=DESC&pageSize=1&page=1

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Why is the helpers folder so important in an Express application?

Exploring the ideal hybrid folder structure to meet our needs is my current project. After sifting through numerous resources and sources, I stumbled upon a folder labeled helpers housing files with a .js extension. |-- app | |-- controllers | | `- ...

What is the best way to track all method calls in a node.js application without cluttering the code with debug statements

How can I automatically log the user_id and method name of every method called within a javascript class without adding logger statements to each method? This would allow me to easily track and grep for individual user activity like in the example below: ...

What could be the reason my Backbone view is failing to render?

Can anyone help me troubleshoot why this template isn't rendering properly in my backbone views? Any insights would be greatly appreciated! Below, I've included the code from my jade file for the views and the main.js file for the backbone js scr ...

When executing the command "npm," a message pops up that says "Error: Module 'inherits' not found."

module.js:340 throw err; ^ Error: Module 'inherits' not found at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380: ...

Using the power of node.js to iterate through a loop of queries and execute

While I am running a for loop, within the loop itself I am executing a PostgreSQL query and storing the result in an array. However, I am unable to determine the order of execution. Here is my code: var array =[]; for(var i = 0 ; i< latitude.le ...

NPM has surprisingly opted for an outdated Node version

Upon running node -v on my Linux system, the output is as expected with v16.7.0 due to the binaries installed on my PATH. However, when a scripts element in my package.json calls node -v, it inexplicably prints v9.11.2. What could be causing this discrepan ...

Decode PDF417 barcode images with ease using HTML and JavaScript on both desktop and mobile web browsers

Looking to utilize JavaScript to decode PDF417 type barcode images? If you're encountering Zxing plugin issues in mobile browsers but it works well on desktop browsers with https://github.com/PeculiarVentures/js-zxing-pdf417, consider alternative sol ...

Node.js version 18 does not support the installation of npm at all

My system has never utilized JavaScript, with plans to switch to TypeScript. However, both npm and yarn are unresponsive. To troubleshoot, I uninstalled Node.js 14 and upgraded to Node.js 18. Additionally, I deleted the npm and npm-cache folders. The ins ...

Feeling uncertain about Node, NPM, Bower, and how to set them up for Bootstrap?

Looking to expand my knowledge in web development, I have a solid understanding of HTML, JS, CSS, and server-side programming. However, the concepts of Nodejs, npm, and Bower are still unclear to me. In order to begin a new project, I created a designated ...

Trouble with value updating in PostgreSQL with NodeJs

var express = require('express'); var app = express(); var pg = require('pg'); var connectionString = "postgresql://postgres:sujay123@localhost:3001/redc"; app.use(express.static('public')); app.get('/index.h ...

Look up the attribute in the parent node and retrieve the value of the child node's attribute

Simplified Version: Find attribute in parent node and retrieve attribute values from child nodes. Detailed Version: Within an XML file, I have the following snippet: <include template="directory/file.xml"> <param name="permission" va ...

Passport.js encountered an issue when attempting to serialize the user for the session

I'm having trouble with the Passport.js module and Express.js. My code is set up for a hardcoded login for testing purposes. However, I keep getting the error message: I've searched online for solutions on Stack Overflow, but I can't seem ...

Ways to trigger an npm script from a higher-level directory?

After separately creating an express-based backend (in folder A) and a react-based front-end project (in folder B), I decided to merge them, placing the front-end project inside the back-end project for several advantages: No longer do I need to manu ...

Steps for removing temporary files with multer

My Node.js application has a feature that allows users to upload files, and the permissions are enforced using Express middleware with the help of multer. The usual route first authenticates the user, checks if they have the necessary permission, and then ...

Struggling to link an external JavaScript file to your HTML document?

While attempting to run a firebase app locally, I encountered an error in the chrome console: GET http://localhost:5000/behaviors/signup.js net::ERR_ABORTED 404 (Not Found) Do I need to set firebase.json source and destination under rewrites or add a rout ...

Guide to importing firebase-functions and firebase-admin using ES6 syntax for transpilation with Babel in Node 10

I've been working on my cloud functions in ES6 and using Babel to transpile them for the Node v10 environment. But, I've come across an odd issue. It seems that when I import firebase-functions like this: import functions from 'firebase-fu ...

I am eager to launch my Node.js Server on Azure App Services

While attempting to deploy my node.js server on Azure using app service, I consistently encounter various errors. The issues range from the build command being unavailable in the configuration to difficulties fetching content. It has become quite frustrati ...

The NPM module known as system-sleep can put your system into an

Help! My code seems straightforward, but only 0 gets printed. The sleep function is causing my program to hang indefinitely. What am I missing here??? import sleep from 'system-sleep'; function starti() { for (var y = 0; y < 10; y++) { ...

Turning Node.js timestamp into MySQL format

Currently, I am using Node (Express.js) to update a MySQL database with the current date. While it is functional, my code seems to be repetitive. let newDate = new Date(); let yearNow = newDate.getFullYear(); let monthNow = newDate.getMonth(); let dayNow ...

An issue arises in the NodeJS Express authentication process when attempting to set headers after they have already been sent,

Currently, I am working on developing a basic authentication system using the POST method. app.post("/api/auth",function(req,resp) { var username = req.body.username||req.param('username'); var password = req.body.password||req.param(&a ...