"Error: 'res' variable is undefined within the EJS template

My task is to dynamically display different templates based on the route, which in this case are /edit and /new. I need a condition to determine whether to show tabs or something else.

In my router.get('/edit' cb) function, I tried checking if res.url.includes('new') returns true. Then, inside my header.ejs file, I implemented a condition:

<% if(res.url.includes('new')) { %>
  <h1>Something else</h1>
<% } else { %>
  <h1>This</h1>
<% } %>

However, an error message saying res is not defined is being displayed. My question is: what is the best approach to solve this issue? My initial thought was to create another header file, but that would involve duplicating a significant amount of code, so I might be over-optimizing it.

Answer №1

Make sure to include this code:

res.render('template.ejs', { res: res });

Double check to see if you have forgotten to pass the object { res: res }

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

Do not include any null or empty objects when assigning to an array in Mongoose

When using mongoose's find() or findOne() methods, the returned value will be [] and null, respectively, if the where conditions are not met. This can cause issues when assigning these values to an array. Currently, I am filtering out the null values ...

Is reCAPTCHA v3 functioning properly?

My front end utilizes vuetify in this manner : validate: async function () { let tokenCaptcha await this.$recaptcha('login').then((token) => { tokenCaptcha = token }) if (this.$refs.form.validate() && tokenC ...

The command could not be located: C:Program Files odejs ode.exe

Something strange is going on. I ran the ionic serve command to start my Ionic server, and I was prompted to update to the latest version of Ionic (3.20.0 => 4.0.0). However, after the update, I started getting an error message every time I ran any ionic c ...

What could be causing the request.body to be considered as undefined?

My server has a node-js setup with bodyparser and other modules: var express = require('express'); var dbcon = require('./app/db/databaseconnection'); var bodyParser = require('body-parser'); var app = express(); var router ...

The error message "strange await can only be used in async functions" is specific to Node.js

I encountered a strange error where I am only allowed to use await in async functions, but I am using it in an async function. What's odd is that I didn't have this error before when I was editing another file, and now suddenly it pops up. I trie ...

error encountered: script not found during starting the application, leading to application crash when attempting to deploy on Heroku

I am a beginner in the world of coding, but I am eager to learn so that I can create a chatbot for my Facebook page. Below are the scripts I have inside each .js file and the errors I encountered. After running 'heroku open', the web displays an ...

When using async functions in iterative processes

In my current setup, I am utilizing a for-each loop to handle a list and specifically require element n to be processed only after element n-1 has completed: let elements = ["item1", "item2", "item3"]; elements.forEach(function(element){ someAsyncFun ...

When attempting to install the cube using npm, an error message was encountered stating "No distribution

Running Ubuntu 14.04 and just installed nodejs, npm, and mongodb. Trying to install cube with npm install cube but encountering the following errors: npm ERR! Error: No dist in websocket-server package npm ERR! at next (/usr/share/npm/lib/cache.js:746 ...

What is the best way to utilize a submodule within a module on Node.js without external help?

Looking to utilize the 'utils-merge' node module that is included as a dependency of express 4.12.3 within my server application. I have successfully installed express on my server. I attempted: var merge = require('express/utils-merge&apos ...

Issue encountered while generating a fresh migration in TypeORM with NestJs utilizing Typescript

I am currently working on a Node application using TypeScript and I am attempting to create a new migration following the instructions provided by TypeORM. Initially, I installed the CLI, configured my connection options as outlined here. However, when I ...

I encountered an issue with a missing token in the client side mode that requires a secret error. Can anyone guide me on the steps

I encountered an issue while working on a client project. The error message I received states, "A secret error is missing in client-side mode, please provide a token." Please find the code snippet below: const getstream = require('getstream'); c ...

Having trouble with node module depths in npm2 on macOS?

For Windows-based projects using node.js, it is advisable to utilize an npm package like flatten-packages to manage directory depth and avoid surpassing the MAX_PATH limitation. More information on this limitation can be found here: https://msdn.microsoft. ...

What are some alternative methods for visualizing memory usage in node.js applications?

I've been working on a project that involves running several persistent processes in node.js. As I continue to develop the project, I am interested in monitoring and charting the memory usage of each process over time to aid in debugging. Although th ...

What methods can we use to determine if the TURN or STUN server has successfully established a connection?

I rely on http://code.google.com/p/rfc5766-turn-server/ for my TURN and STUN server setup. I'm looking to develop diagnostics to determine whether the STUN or TURN servers are connected. I would greatly appreciate any assistance with: 1) Implementi ...

Managing Node/Express.js requests using multiple files

//server.js app.use('/shelf', require('./books').middleware); //books.js var app = new express.Router(); app.post('/books' , function (req, res) { console.log('here'); }); module.exports = app; In the process o ...

leveraging the power of Node.js Express for the next step in

Here's a simplified version of an API function that handles post requests: myFunc(req: express.Request, res: express.Response, next){ let err = 'err detected!'; //validateSometing() returns a boolean value, true if validation pass f ...

The asynchronous nature of async await in Express Node.js is causing functions to execute non-sequ

I am having an issue with running 3 database queries and rendering the results to view using async/await in Node.js. It seems like the queries are not waiting and always sending null objects to the view before they finish executing. I'm not sure where ...

Unable to call Socket.on('connect', function() {}) a second time on the same page

I'm working on developing a basic app with easy login and logout functionalities. When logging in, I join a room and when logging out, I leave the room with socket.leave('room'). The issue arises when I try to log in again on the same page ...

Error encountered while building with npm on a particular device: spawn EACCES

When executing npm run dev with identical source code and configuration on two separate machines, one of them generates the following error: > node build/dev-server.js internal/child_process.js:302 throw errnoException(err, 'spawn'); ...

What steps do I need to follow to execute React/Next code that I have downloaded from GitHub?

I recently obtained a zip file from https://github.com/prgrms-web-devcourse/Team_Price_Offer_FE and extracted its contents. When attempting to launch the program in Visual Studio Code, I inputted the command npm start but encountered an issue. Team_Price_ ...