[英]Why cannot find argument in nest.js
當我使用nestjs
開發API
,我遇到了以下errors
。
似乎必須定義參數。
這是我的controller.ts
@Get()
getdata(@Query() query: { place: string}){
return this.eventService.getEvent(place);
}
這是我的service.ts
async getEvent(eventPlace: string): Promise<any>{
const event = await this.eventRepository
.find({
where: {
place: eventPlace,
},
});
這是我的error
src/event/event.controller.ts:24:39 - error TS2304: Cannot find name 'place'.
24 return this.eventService.getEvent(place);
~~~~~
[4:15:34 AM] Found 1 error. Watching for file changes.
為什么找不到這個論點?
我在getdata
函數中設置了參數。 如果有人有意見,請告訴我。
謝謝
@Query
裝飾器可讓您提取特定鍵。 您可以像這樣定義控制器以獲得更好的體驗:
@Get()
getdata(@Query('place') place: string) {
return this.eventService.getEvent(place);
}
要添加 Jesse 的答案,您可以使用對象解構並執行類似的操作
@Get()
getData(@Query() { place }: { place: string }) {
return this.eventService.getEvent(place);
}
當您想要驗證整個 DTO,但只需要其中的某些字段時,這很有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.