From the course: Mastering Nest.js: Build Scalable Applications with Mastery in Nest.js Framework

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Controller fundamentals (fetching params)

Controller fundamentals (fetching params)

So far, we have seen how to get the products and add the products. Now let's see how to get a single product data by passing the params. Inside the controllers file, I'll define a new get handler with the method, let's say get product. Now the issue is that we have two get decorators in the same controller, and none of them has a unique route path given. So in this situation, nest will consider the first get decorator only and will ignore the second one. So the second decorator will never be executed. But good thing for us, we actually want to get a single product based on its ID. So here in the get, I will set the route path to id. That means the route will be slash products slash id. But remember that we want to pass the actual id in the route params and based on the id we get the product data. So that means the id parameter should be dynamic. And if you have worked with the ExpressJS, there we can set the dynamic segments of route params by giving a colon sign, we will do the same…

Contents