javascript - 在 NestJS Controller 中检索多个参数 (@Param) 给出未定义的值

标签 javascript node.js typescript express nestjs

当在 @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/3undefined 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/

相关文章:

javascript - 在 jQuery 中的 IE8 上意外调用方法或属性访问

javascript - Google 图表未在 x 轴上显示完整的小数前导 0

json - 无法让 xlsx 到 JSON 转换器在 Node/Express 中正常工作

javascript - TypeScript 相当于 React 无状态组件中的 rest/spread Prop

javascript - 在 HTML/CSS/JS Web 应用程序中从 SVG 中删除描边边框

javascript - 如何在我的应用程序中包含和使用 ui-router 而不会出现注入(inject)器错误?

javascript - NodeJs MongoDB 更新错误

javascript - JSON.stringify 缺少属性

typescript - TypeScript 中是否有回调函数类型?

javascript - 在构造函数中使用 plainToClass