0

I am trying to get my routing to work in nestjs for multiple routes, however I can't seem to get the routes to map correctly, this is what I want to achieve:

/user/:id/purchase-transaction/:id

I have generated a user module and a user/purchase-transaction module

But both got registered as:

/user/:id routes and

/purchase-transaction/:id routes

So the question is how to connect the two?

What I have tried to do is, create a Routes array:

const routes: Routes = [
  {
    path: '/purchase-transaction/',
    module: PurchaseTransactionModule,
  },
];

@Module({
  imports: [PurchaseTransactionModule, RouterModule.register(routes)],
  controllers: [UserController],
  providers: [UserService],
})
export class UserModule {}

Inside purchase-transaction.controller I modified the @Controller() to be /

And now this maps to:

UserController {/user}
{/user, POST}
{/user, GET}
{/user/:id, GET} 
{/user/:id, PATCH} 
{/user/:id, DELETE}

PurchaseTransactionController {/purchase-transaction}
{/purchase-transaction, POST}
{/purchase-transaction, GET}
{/purchase-transaction/:id, GET}
{/purchase-transaction/:id, PATCH}
{/purchase-transaction/:id, DELETE}

Instead I want the PurchaseTransactionController to map to:

PurchaseTransactionController {/user/:id/purchase-transaction}
{/user/:id/purchase-transaction, POST}
{/user/:id/purchase-transaction, GET}
{/user/:id/purchase-transaction/:id, GET}
{/user/:id/purchase-transaction/:id, PATCH}
{/user/:id/purchase-transaction/:id, DELETE}

Which I achieved by modifying my routes:

const routes: Routes = [
  {
    path: '/user/:id/purchase-transaction/',
    module: PurchaseTransactionModule,
  },
];

However, I see two problems with this, first is that now I have two params that are :id and second is that, well I already have the UserController is it not possible to extend on top of it?

What would be the correct way of doing this, and should I not use the default :id identifier in this case and rename them to :userId and :transactionId?

1 Answer 1

1

I have generated a user module and a user/purchase-transaction module But both got registered as: /user/:id

it makes sense your :id param can be equal to purchase-transaction how will nest know in which case you are ?

first is that now I have two params that are :id

look a this simple function :

@Get(':myParam/hello/:myParam')
   async test(@Param('myParam') myValue: string) {
   console.log(myValue); 
}

now when you send a GET request to http://domain/firstValue/hello/secondValue myValue will take "firstValue" as value then it will be overwritten to "secondValue" so your first param will be lost. so you need to use :userId and :transactionId as you mentionned.

well I already have the UserController is it not possible to extend on top of it

it is possible, Modules are just for managing codes, what matters are routes. but to avoid any conflicts, what I suggest is to keep your routes like you did :

UserController {/user}
{/user, POST}
{/user, GET}
{/user/:id, GET} 
{/user/:id, PATCH} 
{/user/:id, DELETE}

PurchaseTransactionController {/purchase-transaction}
{/purchase-transaction, POST}
{/purchase-transaction, GET}
{/purchase-transaction/:id, GET}
{/purchase-transaction/:id, PATCH}
{/purchase-transaction/:id, DELETE}

and instead of passing through /user/:userId/ each time, you can pass the userId in the request body.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it will make it simpler, but isnt it more logical to keep the transactions under the user, after all they already belong to the user and we already have the userId in the user routes?
like I said you are free to chose, you can keep them under user but you need to use two differents params that's all

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.