Tips on creating a parameterized SQL query within Javascript

Does anyone know how to properly write a parameterized SQL Query in javascript?

I attempted it myself, but encountered an error.

I even tried another method, yet I'm still facing syntax errors.

let sql =

select * from q_users where firstname=?,[${name}]
;

let sql =

select * from q_users where firstname=?,${name}
;

let sql =`select * from q_users where firstname=?,${[name]}`;
db.query(sql)
.then((data)=>{
    console.log(data);
});

Answer №1

To simplify the process, it is recommended to refer to the detailed documentation - along with gaining a basic understanding of javascript. The code snippet:

let sql =`select * from q_users where firstname=?,${[name]}`;

does not make much sense as it is simply a string with an array appended at the end. It might be beneficial to research more about template literals to comprehend them better and utilize them effectively.

The instructions specify passing two arguments to db.query(). Therefore, you should have something like this:

let sql = 'select * from q_users where firstname=?';
db.query(sql, [name]).then(...);

Furthermore, it is advisable to avoid naming variables like name.

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

Tips for integrating a scripting language to communicate with a database within a kubernetes cluster

After setting up a mysql database deployment and service in Kubernetes using Google Kubernetes Engine, similar to the wordpress example, I am trying to access it via R from another deployment and service in my Kubernetes cluster (r-user-app with 2 containe ...

The URL functions properly in Postman, but encounters errors when used in Node.js

I've encountered an issue with a URL that functions properly in POSTMAN. However, when the same code is used in Node.js, it fails to work: const fetch = require('node-fetch'), express = require('express'), app = expre ...

Can switching to polling data from a SQL database instead of a file in a chat application lead to improved performance?

Currently developing a chat application and considering using a SQL database for it. The challenge is that I've come across conflicting information online regarding the performance of using a DB versus a regular file (such as Text or JSON). User expe ...

JavaScript array remains unchanged

I have a sample of JSON data saved in the "data" variable. Here is the FORMAT : { "0" : {"names":"Pooja, Trivedi"}, "1" : {"names":"Pooja, Rooster"} } My objective is to create a map that can calculate the frequency of each name: Pooja = 2 Triv ...

Stop the Bootstrap accordion from collapsing when clicking on an internal anchor (href)

Is there a way to prevent the accordion from collapsing when opening a new page after clicking on a link? I have included the HTML code below, but I need assistance with the JavaScript to achieve this. I'm facing an issue where the accordion category ...

Javascript puzzle - I have 99 obstacles

...but a malfunction isn't one. Hey there, I am new to learning so I apologize for the seemingly simple question. I'm experimenting with a theoretical logic statement that would work using javascript. For instance: if (issues == 99) { malfunct ...

Filtering data dynamically in a nested array of objects using JavaScript

My task involves utilizing a dynamic array named var arr = ["key1","key2","key3"]. The goal is to filter an array of objects using this array. Here’s an example: var obj = [{"key1":{"key2":{"key3":5}}},{"key1":{"key2":{"key3":7}}},{"key1":{"key2":{"key3 ...

Loading data into a Dojo ItemFileReadStore using Grails and the "render as JSON" method

I have developed a controller method that generates a JSON "file" on-the-fly when the corresponding URL is accessed. This file exists in memory and not saved on disk, as it's dynamically created when the URL is hit. I'm attempting to utilize this ...

In the world of Knockout JS, forget about using e.stopPropagation() because it won't

In my table, I have two actions that can be performed: Click event on the row level and click while checking a checkbox inside that row. However, when the checkbox is checked, I do not want the click event on the row to be triggered. <tbody data-bin ...

JavaScript modal component malfunctioning

I'm having an issue with the bootstrap 4 modal component. Whenever I click the button, the modal window fails to appear. Can someone offer assistance? <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"> ...

Client-side filtering of events in FullCalendar

I found a helpful answer on Stack Overflow that I am trying to use for client-side event filtering. While it works perfectly for newer events, I am facing an issue with filtering events that are loaded from JSON. Below is my JavaScript code: $(document) ...

Struggling to remove a particular row in PHP

I've been attempting to remove a specific row from a table using PHP. I found an online tutorial that demonstrated creating a table with data and a delete button next to each row. However, when clicking the delete button, instead of deleting the row ...

Combine the outcomes of various AJAX requests into one variable

Looking to make 2 recursive API calls to create a JQuery datatables page with data from the range of 2016-2021. The API responses are split based on year filters to bypass the 5000 item list limit set by Sharepoint Online. Struggling to combine all API re ...

Is your pure function component not receiving or responding to input props correctly?

Here is my code snippet: const actionCreators = { action: AppReducer.actionCreators.action } interface GlobalState { user: Model.User | null; } interface InputState { setStashBarWidth(width: number); stashWidth: number; } const Header = ...

NodeJs - Issue: Headers cannot be changed once they are sent & TypeError: req.next is undefined

My goal is to retrieve data from a MySQL database by calling methods to insert and read information. The reason I am doing this is because node.js operates asynchronously. Here is my code: exports.list = function(req, res){ var moduleRows; req.getCo ...

I'm confused why my pinia is still displaying as undefined. Is there a way for my app to pause until pinia has finished loading before I filter the products by ID?

Here is my issue with a Vue single file component that should display products sold by a specific brand ID. Despite fetching the products and filtering them based on the brand.id, the products array remains undefined. <script setup> import { useRoute ...

Using Javascript to save a numeric value and accessing it on a different webpage

I'm encountering an issue with a specific feature on my website. I want users to click on a hyperlink that will redirect them to an application form page. The challenge is ensuring that the reference number (a 5-digit code displayed as a header) is st ...

Storing external API requests in create-react-app's service worker for faster retrieval

I'm in the process of transforming a React web application into a PWA (Progressive Web App). I've made the necessary change in the index.js file - serviceWorker.register();. Everything is functioning properly as I can access the home page and as ...

JavaScript - Issue encountered while reaching the bottom of the webpage

While conducting tests using Firebug/Firefox, I am trying to execute a simple command that will scroll the page to the bottom. Here is the command: window.scrollBy(0,3000); Seems straightforward, right? When testing on certain websites like Yahoo.com ...

I keep seeing this strange [object HTMLSpanElement] appearing on my HTML page

Thanks for the help, the issue has been resolved and I appreciate your valuable time! ...