|
1
|
--- |
|
2
|
sidebar_label: "route-params-must-be-defined" |
|
3
|
--- |
|
4
|
|
|
5
|
export const metadata = { |
|
6
|
title: `route-params-must-be-defined - ESLint plugin rules`, |
|
7
|
} |
|
8
|
|
|
9
|
# {metadata.title} |
|
10
|
|
|
11
|
This rule requires properties accessed on `req.params` in an API route handler to correspond to a `[param]` folder in the route path. |
|
12
|
|
|
13
|
## Severity |
|
14
|
|
|
15
|
`error`. This rule is enabled in the [recommended](../../page.mdx#recommended) preset. |
|
16
|
|
|
17
|
## What it Targets |
|
18
|
|
|
19
|
This rule targets `route.ts` and `route.js` files in your project's `src/api` directory. It reports access to a `req.params` property that has no matching `[param]` folder in the route path. |
|
20
|
|
|
21
|
The following code is reported by the rule, since the route has no `foo` parameter: |
|
22
|
|
|
23
|
```ts title="src/api/admin/products/[id]/route.ts" |
|
24
|
export const GET = (req, res) => { |
|
25
|
const x = req.params.foo |
|
26
|
} |
|
27
|
``` |
|
28
|
|
|
29
|
Instead, access a parameter defined by a `[param]` folder: |
|
30
|
|
|
31
|
```ts title="src/api/admin/products/[id]/route.ts" |
|
32
|
export const GET = (req, res) => { |
|
33
|
const x = req.params.id |
|
34
|
} |
|
35
|
``` |
|
36
|
|
|
37
|
## Why it's Important |
|
38
|
|
|
39
|
A route parameter is only available on `req.params` when a `[param]` folder defines it in the route path. Accessing an undefined parameter returns `undefined`, which often points to a typo or a missing folder. |
|
40
|
|
|
41
|
Learn more in the [Route Parameters documentation](!docs!/learn/fundamentals/api-routes/parameters). |
|
42
|
|
|
43
|
## Fixable |
|
44
|
|
|
45
|
This rule isn't auto-fixable. |