简体   繁体   中英

Why cannot find argument in nest.js

When I develop API by nestjs , I suffered following errors .

It seems argument must be defined.

Here is my controller.ts

  @Get()
    getdata(@Query() query: { place: string}){
    return this.eventService.getEvent(place);
  }

Here is my service.ts

  async getEvent(eventPlace: string): Promise<any>{
    const event = await this.eventRepository
    .find({
      where: {
        place: eventPlace,
      },
    });

Here is my 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.

Why this argument cannot be found ?

I set argument in getdata function. If someone has opinion, please let me know.

Thanks

The @Query decorator lets you extract a specific key. You can just define your controller like this to get a better experience:

@Get()
getdata(@Query('place') place: string) {
    return this.eventService.getEvent(place);
}

To add on to Jesse's answer, you could use object deconstructing and do something similar like

@Get()
getData(@Query() { place }: { place: string }) {
  return this.eventService.getEvent(place);
}

This is useful when you want to validate an entire DTO, but only need some of the fields from it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM