Heisenberg
Heisenberg

Reputation: 5299

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

Upvotes: 0

Views: 1817

Answers (2)

Jay McDoniel
Jay McDoniel

Reputation: 70151

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.

Upvotes: 0

Jesse Carter
Jesse Carter

Reputation: 21147

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);
}

Upvotes: 2

Related Questions