Daniel Santos
Daniel Santos

Reputation: 15778

NestJS - A method handler is interfering into another handler with same name in another controller

I had two controllers into two differnt folders into two different modules, Both of them has a method with same create name.

/admin/entity.controller.ts

@Controller("admin")
export class EntityController{
    @Post()
    public async create(@Request() request: any): Promise<List> {
        console.log("request", request) // Logs the Body {"name": "test"} instead of request.
    }
}

/user/entity.ontroller.ts

@Controller("user")
export class EntityController{
    @Post()
    public async create(@Body() entity: Entity) {
        console.log("entity", entity) // logs the body {"name": "test"}
    }
}

When I do post the following in both routes: {"name": "test"},

It happens that admin create() method logs the body part, not the requests as expected.

It seems to be that the definition of the user controller is interfering into the admin controller and making it to do not work properly.

Is this an expected behaviour?

Upvotes: 1

Views: 1065

Answers (2)

Duncan Lukkenaer
Duncan Lukkenaer

Reputation: 13914

This is actually a bug in NestJS, which will be fixed in version 8.0.0.

Nest currently uses class names to identify providers/controllers/injectables. The fix uses class references instead.

Upvotes: 1

Jay McDoniel
Jay McDoniel

Reputation: 70101

You're overriding the EntityController depending on how Nest pulls in the dependencies. Change the name of one of the classes. AdminController UserController or something

Upvotes: 3

Related Questions