A Guide to Retrieving Parameters and Request Body using Express and Typescript

When I use the PUT method, I encounter this issue:

const createFaceList = (req: Request<{faceListId : string}>, res: Response, next: NextFunction) => {

console.log(req.body.name);
console.log("faceListID = " + req.params.faceListId);

addFacelist(req.params.faceListId, req.body)
.then( result => {
    return res.status(200).json({result})
})
.catch(err => {
    logging.error(NAMESPACE, err.messagem, err);

    return res.status(err.statusCode).json({
        statusCode: err.statusCode,
        message: err.message
    })
})
}

My console output displays undefined values:

undefined
faceListId = undefined

Could someone provide insights on how to resolve this issue? Thank you.

Answer №1

define interface TypedRequestBodyParams<Params, ReqBody> extends Express.Request {
    body: ReqBody,
    params: Params
}

const generateFaceList = (req: TypedRequestBodyParams<{faceListId : string}, {name: string}>, res: Response) => {
    const { faceListId } = req.params
    const { name } = req.body
}

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

How to apply dynamic styling to a MatHeaderCell using NgStyle?

My goal is to dynamically style a MatHeaderCell instance using the following code: [ngStyle]="styleHeaderCell(c)" Check out my demo here. After examining, I noticed that: styleHeaderCell(c) It receives the column and returns an object, however ...

How can one effectively extract data from a database in a React application?

We have a basic react app with authorization, utilizing JWT tokens and Redux for state management. Personally, I find the code quite complex and obfuscated, making it challenging to grasp fully. However, let's set that aside. Upon login, a token is s ...

Having trouble fixing TypeScript bugs in Visual Studio Code

I am encountering a similar issue as discussed in this solution: Unable to debug Typescript in VSCode Regrettably, the suggested solution does not seem to resolve my problem. Any assistance would be greatly appreciated. My directory structure looks like ...

Delivering items to the handlebars template page

Just starting out with express and handlebars. I am trying to send a JavaScript object and display its values on the webpage. The object is being sent based on the result of a MySQL query: con.query("SELECT * FROM users", function (err, result, fields) { ...

Generating a file using buffer information in node.js

From the front-end client side, I am sending a file to the server. On the server-side, the data received looks something like this: { name: 'CV-FILIPECOSTA.pdf', data: <Buffer 25 50 44 46 2d 31 2e 35 0d 25 e2 e3 cf d3 0d 0a 31 20 30 20 6f 6 ...

Is there a way to enable live-reload for a local npm package within a monorepo setup?

Currently, I am in the process of setting up a monorepo workspace that will house a Vue 3 application (using vite and TypeScript), cloud functions, and a shared library containing functions and TypeScript interfaces. I have successfully imported my local ...

Add a class individually for each element when the mouse enters the event

How can I apply the (.fill) class to each child element when hovering over them individually? I tried writing this code in TypeScript and added a mouseenter event, but upon opening the file, the .fill class is already applied to all elements. Why is this ...

Exploring additional parameters within express.js

I'm currently working on creating an email sender that includes all necessary components such as 'from', 'to', 'subject', 'textBody', etc. However, I am facing a challenge in setting optional parameters in expre ...

How to perform a fetch on a local path in Next.js?

Is there a way to use the fetch method with a relative path like this: export async function getServerSideProps() { // Fetch data from local API const res = await fetch(`/api/get_all_prices`) const data = await res.json() // Pass data to th ...

Troubleshooting Issue: Angular 6 - Authentication token interceptor not including headers

After experimenting with various approaches using Angular 6 for the past couple of days, I recently tried implementing a solution mentioned in this post: . Despite my efforts, the header in the requests still remains unset. import {Inject, Injectable} fro ...

Traversing an Array Using Node.JS and EJS

I am currently working on creating a todo application using Node.js with EJS as the templating engine. I have organized all the information into three arrays: subject, date, and detail. Below is the code snippet used in the express route: app.get('/ ...

What could be the reason for the defaultCommandTimeout not functioning as expected in my script

Is there a way to wait for only one particular element in Cypress without having to add wait commands everywhere in the test framework? I've come across the solution of adding defaultCommandTimeout in the cypress.json file, but I don't want it t ...

The MEAN application encountered a crash due to a loss of internet connection

I'm currently working on a MEAN application. Everything runs smoothly when accessing it through http://localhost:3300 with an active internet connection, but I encounter the following error when disconnected from the internet: process.nextTick(functi ...

Trouble communicating between React app and local Express server - CORS issue causing the problem

My React application is running on localhost:3000 Meanwhile, my Express server is operating on localhost:1234 Here's the setup for my Express server: const express = require("express") const cors = require("cors") const app = express(); const PORT ...

Troubleshooting issue with getServerSideProps not functioning in Next.js while utilizing Next-redux-wrapper and TypeScript

When attempting to trigger an action as outlined in the documentation using the getServerSideProps function with the help of next-redux-wrapper store and redux-thunk, I am encountering the following TypeScript error: ts(2322): Type '({ req }: GetServe ...

Creating mock objects with Jest

I am currently delving into the world of jest testing. Here is a snippet from an implementation class I'm working with: import { ExternalObject } from 'external-library'; export class MyClass { public createInstance(settings : ISettings) ...

Tips for sharing common variables across all routes in Express by rendering them from app.js

Within my Node.js application, there are several variables that need to be rendered on every route. With approximately 4-5 common variables across about 20 routes, I find myself repeating the code for passing these variables in each res.render. I am seeki ...

Display index.html regardless of where it is located

I have a Node.js app using Express and I want the index.html file to be displayed to the user regardless of the URL they are on. For example, if someone goes to http://example.com, they should see the same page as if they went to http://example.com/test. ...

Struggling to locate the correct path for the CSS file in Express and Handlebars

As part of a student exercise, I am attempting to access an API containing information about presidents and style them using CSS. However, I am facing issues with linking the style.css file. I have tried various methods to change the path and public path ...

Fetching data from sub-arrays in Node.js Express using the populate method

There is a hierarchical structure in my relative field where subRelatives are nested within each other, creating a cascading array. Sometimes this nesting occurs up to 10 levels deep. The following is the code snippet: router.get(`/userTree/:id`, async (r ...