Ways to retrieve parameters in the following function using Express

I need help accessing fromFlight and toFlight variables in the next function that I have defined. Currently, I am only getting the output of the entire req object. Can someone assist me with this issue? Below is a snippet of my code:

const express = require("express");
const app = express();
const port = 8080;
app.listen(port, (err) => {
 if (err) {
  console.log("connect error", err);
 }
 console.log("server is up");
});
app.use(express.json());
app.all('/',(req,res)=>{
  res.send("welcome to home");
});
app.get('/getflightReturn/:fromFlight-:toFlight',(req,res,next)=>{
 console.log(req.params);
 let fromFlight = req.params.fromFlight;
 let toFlight = req.params.toFlight ;
   next(fromFlight,toFlight);
},
 (req,res,fromFlight,toFlight)=>{
   //this is next function 
   console.log(fromFlight,toFlight); 
  //res.send(fromFlight,toFlight);
 }
);

Answer №1

Include fromFlight and toFlight in the request, not after it as shown below

req.fromFlight = fromFlight;
req.toFlight = toFlight;

This will allow you to retrieve it like this

console.log(req.fromFlight)

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

The static variable within the class remains unassigned when the callback function is invoked during app.listen

As I make the transition from JavaScript to TypeScript for developing my node.js applications, I find myself facing some confusion. For instance, I currently have this code snippet that initializes an express server: import * as express from 'expres ...

The MySql db.query command in NodeJS consistently seems to be executing last

In my NodeJS and Express Framework code utilizing MySql database, I encountered a situation where the lines "Start line" and "End line" were executed before the db.query command. See below: app.get('/getusers', (req, res) => { console.lo ...

What is the best way to compare two strings in an Angular.js view?

When working with two ng-repeat elements, I encountered an issue where I only want to display data when the text value of both array elements match. I attempted to use ng-show, but it does not stop the condition when the values match. <div ng-show="a ...

Tips for establishing a fixed point at which divs cease to shrink as the browser size decreases

There are numerous dynamically designed websites where divs or images shrink as the browser size decreases. A great example of this is http://en.wikipedia.org/wiki/Main_Page The div containing the text shrinks proportionally to the browser size until it ...

A guide to managing entity data in Google Datastore with Node.js

I am facing issues when it comes to setting and retrieving entity data from the Google Datastore. The various examples I have come across are confusing me, and I'm unsure which one is correct. Below is a snippet of the code I have been working on, but ...

Creating a dynamic duplication feature for a form's text area

As a newcomer to the world of Javascript, I am embarking on creating a review-writing page. However, I have hit a roadblock in my progress. The main issue I am encountering is implementing a button that allows users to add a new section by duplicating an ...

Locate a deeply nested element within an array of objects using a specific string identifier

Trying to search for an object in an array with a matching value as a string can be achieved with the following code snippet. Is there an alternative method to optimize this process without utilizing map? Code: const arr = [{ label: 'A', ...

Tips for implementing validation in JavaScript

I'm brand new to learning Javascript. Recently, I created a template for a login page and it's working perfectly fine. However, I am struggling with setting up validation and navigation. My goal is to redirect the user to another page if their us ...

What is the best way to extract the property name from the AJV output in order to effectively translate validation errors into user-friendly

I am currently utilizing the AJV library for input validation in my nodejs express api. I'm facing an issue with extracting the property name associated with each error object within the returned array. [{ instancePath: '', schemaPath: & ...

Ascending to the Peak within a div

<script type="text/javascript"> $(document).ready(function(){ updateContent(); }); function updateContent(){ $('#mainDiv').load('home.php', function(){ scrollToTop(); }); } ...

How can I utilize passed in parameters in Meteor React?

I am trying to figure out how to use two params that I have passed in the following example. Can someone please assist me? updater(layer, item){ this.setState({layer5: <img id="layer5" className="on-top img-responsive center-block" name="layer5" ...

Images showing Strava heat maps retrieved through API

Check out this amazing heatmap created by Strava! I'm curious about how they were able to achieve this - it seems like they are using the API to request overlay images based on the network tab. I have my own geo data, but I'm wondering how I can ...

The concept of recursion in AngularJS directives

I developed a unique custom directive. angular.module('menu',[]) .directive('MenuDirective',function(){ return{ restrict:'E', replace:'true', scope:{ m ...

Programmatically searching individual columns in Datatables is a powerful feature that

I am currently working on creating a jQuery datatable with search functionality in each column, using the example provided on the datatables page found at https://datatables.net/examples/api/multi_filter.html Specifically, I want to be able to search the ...

Adding fields from one collection to another collection in MongoDB based on specific conditions for a considerable amount of data

I encountered a situation where I constantly need to update a large number of collections. Here are the collections: coll1 { "identification_id" : String, "name" : String, "mobile_number" : Number, "location" : String, "user_properties" : [Mixe ...

Content in TinyMCE styled with the default CSS of the application

Hello fellow developers; I'm struggling to find a suitable solution to apply the same styles from our main CSS file to the text entered in the TinyMCE editor. Just to clarify, I don't want to alter the overall theme or appearance of TinyMCE itse ...

Eliminate all key-value pairs from an array of objects except for the specified key-value pair

I've got an array filled with objects const myArr = [ {k1: 1, k2: 1, k3: 3, k4: 4}, {k1: 1, k2: 2, k3: 3, k4: 4}, {k1: 1, k2: 2, k3: 3, k4: 4}, {k1: 1, k2: 2, k3: 3, k4: 4} ] I'm attempting to filter these objects, although I don&apos ...

Operate on Nested JSON Arrays

I have the following JSON array: var salesData = [ { "products": "Cars", "solddate" : "2022-01-01", "noofitems" : " ...

tips for showcasing an item in a tooltip within a data table

I am working on dynamically creating a table with data retrieved from an ajax response. My goal is to display the data stored in an object within a tooltip attached to each cell. Currently, I have successfully rendered the table, but it is displaying `[obj ...

What circumstances could lead to a timeout while executing sequelize in a Node.js environment?

Within my application built with Node.js version 10.15.0, I utilize the sequelize package (version 4.44.3) to establish a connection to a remote MySQL database. This application operates within a Docker container. However, after prolonged usage, I encounte ...