manipulator route in Nest.js

I have the following PATCH request:

http://localhost:3000/tasks/566-344334-3321/status
.

The handler for this request is written as:

@Patch('/:id/status')
updateTaskStatus() { // implementation here 
    return "got through";
}

I am struggling to grasp the workings of the stem part and how the appropriate handler is mapped to process the request.

For instance, in /566-344334-3321/status
The id section is /566-344334-3321 which is variable.
However, the fixed suffix /status must be precise.
Any deviation results in an error message: "error": "Not Found".

What explains this behavior?

Answer №1

To obtain the `id` parameter, utilize the `@Param` decorator to map the `:id` in your request to a typescript number.

Here is the correct way to implement the `updateTaskStatus` function as per the guidance provided in the NestJS documentation on Route Parameters

@Patch('/:id/status')
updateTaskStatus(@Param('id') id: number) { 
    return `Received ${id} successfully`;
}

If you intend to use `status` as a dynamic value similar to `id`, ensure to annotate your parameter with the same decorator

@Patch('/:id/:status')
updateTaskStatus(@Param('id') id: number, @Param('status') status: string) { 
    return `Received ${id} and ${status} successfully`;
}

The NestJS documentation is comprehensive and valuable, make sure to explore the documentation related to controllers

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

Passing a type as an argument in Typescript

How can I pass a type as a parameter in Typescript? type myType = {} const passingType = (t: Type) => { const x : t = {} } passingType(myType); I keep receiving TypeScript errors. 't' is referencing a value, but it is being used as a t ...

Error encountered: ⨯ Compilation of TypeScript failed

My current project is utilizing Node.js in conjunction with TypeScript An issue has arisen during compilation within my Node.js application: E:\NodeProjects\esshop-mongodb-nodejs\node_modules\ts-node\src\index.ts:859 ret ...

Navigating through the directories in PUG using the absolute path

Referring to the docs for PUG (), it states: If a path is absolute (example: include /root.pug), it gets resolved by prepending options.basedir. Otherwise, paths are resolved in relation to the file being compiled. To clarify, I understand that this in ...

The webpage may not load correctly if the URL contains three or more slashes

Whenever I input a URL like "http://127.0.0.1/ab" or "http://127.0.0.1/ab/cd", the webpage displays correctly. However, when I add a third slash like "http://127.0.0.1/ab/cd/ef", the resulting page does not render properly. Th ...

Developing a bespoke React Typescript button with a custom design and implementing an onClick event function

Currently, I am in the process of developing a custom button component for a React Typescript project utilizing React Hooks and Styled components. // Button.tsx import React, { MouseEvent } from "react"; import styled from "styled-components"; export int ...

Ways to dynamically configure Angular form data

Below is an Angular form group that I need help with. My goal is to initialize the form and if there is no data coming into the Input() data property, then set the form values as empty strings '' for user input. However, if there is indeed form d ...

In TypeScript, the first element of an array can be inferred based on the second element

Let's consider a scenario where we have a variable arr, which can be of type [number, 'number'] or [null, 'null']. Can we determine the type of arr[0] based on the value of arr[1]? The challenge here is that traditional function ov ...

Dealing with Worker Process Response in Node.js

In my endeavor to develop a service incorporating the Command Query Responsibility Segregation Pattern (CQRS) in NodeJs, I have devised a specific strategy for segregation: I am setting up distinct workers for querying and executing commands These worker ...

Obtaining a response from an Express server for an AJAX query in Nuxt.js is essential for

Need help with extracting data from a node/express server after sending an ajax query from any page of the nuxtjs app. Typically, I would use the following code to handle ajax queries in a PHP server: $_GET['var']; echo json_encode('Server g ...

The video is not displaying on the webpage when connected locally, but it appears when the source is a URL

Recently, while practicing some basic tasks on a cloud IDE called Goorm, I encountered an issue with displaying a video on a simple webpage. The EJS file and the video were located in the same folder, but when I set the src attribute of the video tag to "m ...

How can I display an agm-polyline within a map in Angular 7?

I need assistance with adjusting the polylines on my map and dynamically setting the zoom level based on their size and position. Here is the code I am currently using: <agm-map style="height: 500px" [latitude]='selectedLatitude' [longitude ...

After encountering an error, the puppeteer promptly shuts down the page

During my page testing, an error is thrown by a dependency. Although the error is not critical and does not impact my application, when testing with Puppeteer and encountering this error, it abruptly closes the tested page. How can I bypass this error to c ...

Tips for efficiently utilizing mapActions in vue with Typescript class components!

Can someone please provide guidance on the correct way to use ...mapActions([]) within a Typescript vue class component? This is my current approach: <script lang="ts"> import { Component, Prop, Vue } from "vue-property-decorator"; import { mapActi ...

Issue with error handling in Nodemailer within Express is not functioning as expected

I have developed a script called MailRead.js to handle reading emails with nodemailer: exports.sendMailService = async (mailOptions) => { return new Promise((resolve, reject) => { mailOptions.from = '<a href="/cdn-cgi/l/email-prot ...

Whenever an attempt is made to perform a POST request, the connection is consistently reset

I'm relatively new to Node JS and could use some guidance on troubleshooting my POST request. I've noticed that it sometimes resolves successfully, but other times I receive an ECONNRESET error. Can someone please help me figure out what might be ...

Using Sequelize to send data from the client-side to the server-side

I am currently developing a project for a fictional library database and website interface. I am facing an issue where only 2 out of the 4 new loan form inputs are being passed to the req.body. Even though all items have a name attribute, it seems like onl ...

The functionality of connect-flash in Express JS is compromised when used in conjunction with express-mysql-session

I am facing a unique issue in my project. I have identified the source of the problem but I am struggling to find a solution. My project utilizes various modules such as cookie-parser, express-mysql-session, express-session, connect-flash, passport and m ...

Error: The version of @ionic-native/[email protected] is not compatible with its sibling packages' peerDependencies

When attempting ionic cordova build android --prod, the following error occurred: I have tried this multiple times. rm -rf node_modules/ rm -rf platforms/ rm -rf plugins/ I deleted package.lock.json and ran npm i, but no luck so far. Any ideas? Er ...

Successive type label

Looking to create an object that can have either primitives or objects as properties? Avoid pitfalls like the following: const obj: DesiredType = { correctProp1: 'string', correctProp2: 123, correctProp3: true, wrongProp4: [1, 2, 3], pr ...

Utilize the express library (NodeJS, TypeScript) to send back the results of my function

I am curious about how I can handle the return values of my functions in my replies. In this specific scenario, I am interested in returning any validator errors in my response if they exist. Take a look at my SqlCompanyRepository.ts: async create(compan ...