When a variable is used in Postgresql, the ORDER BY clause is disregarded, but accurate results are returned when the variable value is

After investigating, it seems that the query is always returning rows ordered by id, regardless of the value passed in the sortType variable (verified in console).

export async function fetchAnimalList(sortType) {
  noStore();

  try {
    const areas = await sql<Animals>`
    SELECT e.id, e.name, e.slug, e.sliderarray, e.location, e.classification1, a.name AS locationname
    FROM animals e
    JOIN areas a ON (e.location = a.displayorder)
    ORDER BY ${sortType}
  `;
    return areas.rows;
  } 
}

The code snippet calling the function with the variable looks like this:

const animalList = await fetchAnimalList('e.name');

When manually replacing ${sortType} with 'e.name', the result is as expected - ordered by e.name. What could be causing this unexpected behavior?

Answer №1

When attempting to use a parameter as an identifier, such as a column name, it is not effective. The variable ends up being replaced by a string constant in the query:

SELECT ... ORDER BY 'e.name'

Sorting based on a constant value will yield no impact on the results.

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

Seeking assistance with TypeScript promises

Just starting out with typescript and nodejs, but I've got to tackle some issues in the typescript code. I'm looking to execute an ECS one-off task using Pulumi. I have the documentation on how to run the task from the taskDefinition, which can ...

Querying JSON with Node.js and PostgreSQL

Utilizing node-pg, my goal is to search for a specific string within a JSON object. For example, here is a snippet from a row: { viq_id: '801583', title: 'Blank, security key, lock system, and production method', applicants: [ ...

Is there a method to verify the status of an onSnapshot listener?

There have been instances when I've observed that the link between the browser tab and Firestore gets disconnected for unknown reasons, leading to unsuccessful re-connection. Is there a reliable method to determine if your onSnapshot is still activel ...

I'm having trouble importing the java.sql.* package into my Eclipse project

https://i.sstatic.net/uX80x.png While I can successfully import other packages like java.io into Eclipse, I am facing issues importing java.sql into my project. What could be causing this problem? I'm feeling stuck and unsure of how to proceed. Desp ...

Tips for enabling users to import from subdirectories within my NPM package

Is there a way to allow users to import from subfolders of my TypeScript NPM package? For instance, if the TypeScript code is structured like this: - lib - src - server - react Users should be able to import from the subfolders as package-name/react, ...

Is it a good idea to separate TypeScript types into their own package?

In my React/TypeScript application, I have approximately 100 files where various types are declared. I am looking for a simpler and more automated approach to extract all these types into a separate package. Is there a method other than manually copying ...

Tips for adjusting the zIndex of the temporary drawer component in TypeScript

Currently, I am working on incorporating a "temporary" drawer that is clipped under the app bar. Please note that the drawer variant is set to 'temporary' and it needs to be clipped under the app bar. If you need more information, refer to my pr ...

Retrieving information from Strapi using a bearer token and SWR in Next.js

Hey there, I'm currently using Strapi for my CMS and Next.js for the frontend. I'm utilizing SWR and Axios for data fetching purposes. I am attempting to retrieve data from my Strapi backend using a bearer token, and here is the code I have so fa ...

Creating a velocity gauge style graph in Angular 4: A step-by-step guide

Hello, I'm currently working on building a speedtest-inspired application. While everything else is going smoothly, I'm struggling to incorporate a speedometer-like chart in Angular 2/4. Despite searching extensively, I've only come across J ...

Sorting through items within a container object

I am currently working with an object named 'docs' that contains multiple objects within it. I am attempting to determine the count of entries that have specific values for both 'exopp_rooms_id_c' and 'is_active'. While this m ...

What causes the oninput event to act differently in Angular as opposed to regular JavaScript?

As I delve into learning Angular with TypeScript, I've encountered some inconsistencies compared to JavaScript that are puzzling me. Take for example this function that works flawlessly with pure JavaScript, where it dynamically sets the min and max a ...

In Typescript, you can extend an interface with the Node type to specifically

I'm currently utilizing Cypress 10. I came across the following code snippet: Cypress.Commands.add( 'byTestId', // Taking the signature from cy.get <E extends Node = HTMLElement>( id: string, options?: Partial< ...

End the pipeline execution in a chain of RxJS observables

Here is a scenario where a series of Observables are chained together. Is it possible to prevent the subsequent chain from executing if an error is thrown by 'parentObs1'? import { throwError } from "rxjs"; import { mergeMap, catchError ...

Building a dropdown menu component in react native

Looking to implement a dropdown menu in React Native using TypeScript. Any suggestions on how to achieve this for both iOS and Android platforms? Check out this example of a dropdown menu ...

How to update an Angular 2 component using a shared service

My question is regarding updating components in Angular 4. The layout of my page is as follows: Product Component Product Filter Component Product List Component I am looking to link the Product Filter and Product List components so that when a user c ...

Ways of invoking a component method from a service in Angular 2

I have a concept for creating a unique service that is capable of interacting with one specific component. In my application, all other components should have the ability to call upon this service and then have it interact with the designated component in ...

What is the correct way to extract results from an Array of Objects in Typescript after parsing a JSON string into a JSON object? I need help troubleshooting my code

Here is my code for extracting data from an array of objects after converting it from a JSON string to a JSON object. export class FourColumnResults { constructor(private column1: string, private column2: string, private column3: string, priv ...

What is the best way to create a straightforward interface using TypeScript?

Although I'm sure this question has been asked before, I couldn't find the answer on Google or SO. So here it goes: I am looking to create an interface with a key named id of type number. Additionally, there may be other keys with unknown names ...

The 'DOCUMENT' module (imported as 'i23') could not be located within '@angular/platform-browser'

During my upgrade from Angular version 7 to 8, I encountered an error when building the project even though I am not using DOCUMENT. It seems like something is causing this issue that I am overlooking. I have thoroughly checked all the files and confirmed ...

Strategies for addressing the issue of assigning "xx" to intrinsic attributes and props in React with TypeScript

I'm facing an issue where I am unable to locate 'count' and assign {count: number; title:string} type to IntrinsicAttributes in a React and TypeScript environment. There are two components involved, ParentComponent and ChildComponent. In t ...