当在 @Controller()
装饰器中定义时,我无法从 NestJS Controller 检索参数(controller.ts 中的 @Param()
装饰器)参数和 @Get()
参数。
我是 NestJS 的新手,所以我可能错过了一些东西,但是谁能向我解释一下如何获取 /folder/1/doc/2/file/3
uri 并没有给我所有的信息我想要的参数?
相关代码和框在这里:https://codesandbox.io/s/nest-typescript-starter-zbvfw?fontsize=14
@Controller('folder/:folderId/other|doc/:docId/file/:fileId')
当我点击 /folder/1/doc/2/file/3
时 undefined 2 3
我希望 1 2 3
> 乌里
提前谢谢你。
最佳答案
这是因为您的 or-pipe |
位于顶层,这意味着您的 URL 的第一部分或最后一部分被解释。您可以使用 this tool 检查您的路径生成的正则表达式:
/^folder\/(?:([^\/]+?))\/(?:(other|doc))\/(?:([^\/]+?))\/file\/(?:([^\/]+?))\/?$/i
相反,您必须在 or 表达式两边加上括号:
@Controller('folder/:folderId/:type(other|doc)/:docId/')
由此,type
是另一个具有两个匹配值的命名参数。它将包含 "other"
或 "doc"
。
关于javascript - 在 NestJS Controller 中检索多个参数 (@Param) 给出未定义的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56938026/