Show the results of an SQL query on an EJS file with Node.js Express

Hey there, just starting out and looking to showcase my SQL query results in my EJS file. Struggling a bit with this.

I'd really appreciate any guidance or assistance on this. Thanks!

Answer №1

For utilizing ejs, it is necessary to configure Express render engine.

app.engine('html', require('ejs').renderFile);

Subsequently, you can send variables to templates using a SQL query:

connection.query('SELECT 1', function (err, rows) {
  res.render('file.html', {
    'sql': rows
  });
});

Within your template, you can employ code like this:

<%= sql.property %>

You are also able to include scripts within tags:

<% sql.forEach(function(field){ %>
  <%= field.property %>
<% }) %>

Answer №2

Utilize the EJS template URL and transmit SQL outcomes by using:

.render()

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

Requiring three parameters, yet received four

Encountering an error in the dashboard.tsx file while trying to implement a line of code: "const { filteredEvents, stats, tableApps, formattedDate } = filterData(dataAll, Prefix, listApp, dateSelected);" The issue arose with the dateSelected parameter resu ...

Encountered a hiccup when trying to launch a duplicated Node.js project on my

Hello, unfortunately I am encountering yet another issue after cloning a project from GitHub onto my PC. When running the project, an error is displayed. Does anyone have a solution for this problem? The error message reads: "C:\Program Files (x86)&bs ...

How to exclude a field in a mongoose query?

Within my User model, I have properties such as name, password, resetToken, and resetTokenExp. Whenever querying a user (using findById, find({}), etc.), I want to exclude the password, resetToken, and resetTokenExp fields. User.findById(id) .select(&a ...

Managing POST request data in Express: A step-by-step guide

Currently, I am facing an issue with my alert button on the client side, which has an event listener that is supposed to send data to the server. Below is the code snippet for the client side: alertBtn.addEventListener("click", () => { axios ...

The TypeScript error message states that a value of 'undefined' cannot be assigned to a type that expects either a boolean, Connection

I've been grappling with this code snippet for a while now. It was originally written in JavaScript a few months back, but recently I decided to delve into TypeScript. However, I'm struggling to understand how data types are properly defined in T ...

Why is the keys prop in the cookie-session library important for ExpressJS?

Currently, I am delving into the realm of back-end programming, focusing specifically on Node.js and ExpressJS. One aspect that has me scratching my head is the purpose of the "keys" prop in the cookie-session library. Despite consuming vast amounts of inf ...

How do you display a GET request response onto a Jade page?

After successfully making a GET request to an API, I am now looking for a way to display the response on a Jade page. Below is the code snippet from 'index.js' located in the routes folder where the GET request is implemented. Any suggestions on ...

How can I retrieve arguments/parameters from an Allure report while it is running?

Within our testing framework, we utilize the allures .addArgument method to include a parameter in the report indicating which team is responsible for a specific test. AllureReporter.addArgument('teamName', 'myTeam'); We have numerous ...

Step-by-step guide on integrating node.js and MySQL to store data from an online form in a database

Currently, I am attempting to insert data into a MySQL database using node.js by clicking the submit button. However, an error message has appeared and despite understanding it somewhat, I am unsure of how to proceed. Any assistance would be greatly apprec ...

Using the .sendFile method is only compatible with the Chrome browser

I'm encountering an issue when trying to send a PDF file to a client using .sendFile from Express. Everything works perfectly in Chrome - with a Download link, Chrome saves the file; however, with a normal _blank link, Chrome opens it in a new tab. B ...

Pairing strings with arrays

I'm trying to compare elements in two arrays and see if there are any matches. However, I can't seem to get it working correctly. let name; let list = ["Kat", "Jane", "Jack"]; // sample array let input = ["Hey", "i'm", "Jack"]; if (input.fo ...

Error encountered when attempting to insert data into a PostgreSQL database using Node.js and Sequelize

I'm currently using the node sequelize library to handle data insertion in a postgress database. Below is the user model defined in the Users.ts file: export class User extends Sequelize.Model { public id!: number; public name: string; public ...

Building a Personalized Docker Image for Node.js and Selenium-Webdriver: Step-by-Step Guide

Having trouble getting my automation project to run in Jenkins. Issues with Docker images downloaded from the Internet and Linux agent compatibility with my Windows environment. I manually downloaded Google Chrome/Chromedriver in the build, but still can& ...

Finding it difficult to install the grpc-tools package using npm or yarn on a Mac M1 chip?

Steps for Installation: npm install -g grpc-tools yarn add global grpc-tools Encountered errors while attempting to install grpc-tools on Mac M1 Big Sur. The error messages are displayed below: npm ERR! code 1 npm ERR ...

Sporadic error message: EADDRINUSE :::5000

There are times when rebooting my server resolves the issue, while at other times it doesn't. I keep encountering the error message Error: listen EADDRINUSE :::5000. In the package.json file of my client, I have set a proxy to "proxy": "http://localho ...

next() does not process the middleware sub-stack in route.param

In the scenario below, when making a GET request to /api/users/i, the secondMw middleware is not executed even though there is a next() call in the firstMw. Why does this happen and how can I ensure that the secondMw runs? var apiRouter = require('ex ...

Can Restify Node return an integer value?

https://i.stack.imgur.com/72ltz.png I recently encountered an issue while trying to validate my URL with Facebook for my bot. It appears to be related to a data type problem since I seem to be sending a string instead of an integer. Below is a snippet of ...

Is there a way to automate the duplication/copying of files using JavaScript?

I have a GIF file stored in the "assets" directory on my computer. I want to create multiple duplicates of this file within the same directory, each with a unique filename. For example: If there is a GIF file named "0.gif" in the assets directory, I woul ...

passport not initializing session with requests

I am currently utilizing passportJS for managing login and persistent sessions on the server backend. While everything functions properly when sending requests from the server frontend (a webpage that I didn't create and lack sufficient knowledge to ...

I'm trying to establish a connection to MongoDB within the getStaticProps function in Next.js. Can anyone

Hello, I am a beginner in next.js and I have successfully rendered the home page on the server by populating the props object through a file named "products.json". Now, my next goal is to populate the props object in the function called getStaticProps usin ...