#Guard Issue ..its not running
1 messages · Page 1 of 1 (latest)
this is the interface
which is extending the express request
and i m adding this all() property using the guard
this is the implementation of it
`@Injectable()
export class RequestGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
this.bindRequestHelpers(context.switchToHttp().getRequest());
return true;
}
/**
- Bind Request Helpers
- @param request
*/
bindRequestHelpers(request: any): any {
const all = function (): Record<string, any> {
return {
...request.query,
...request.body,
...request.params,
};
};
request.all = all;
return request;
}
}
`
in the main.ts file i have used this guard
app.useGlobalGuards(new RequestGuard());
and i have tried consoling the all() property in the guard and it's working
its mean request is flowing in it
BUT
when i try to get this all() property in my controller then it showing
Cannot read properties of undefined (reading 'all')
That's how i m calling it
`import {
Controller,
Get,
Post,
Param,
Body,
Req,
Res,
UseGuards,
} from '@nestjs/common';
import { RequestGuard } from 'src/core/guards/request.guard';
import { Request } from 'src/core/http/Request';
import { Response } from 'src/core/http/Response';
@UseGuards(RequestGuard)
@Controller('customers')
export class CustomersController {
constructor(private customersService: CustomersService) {}
@Get('/order-data/:id')
async OrderData(@Param('id') id: string, req: Request, @Res() res: Response) {
console.log(req.all());
const data = await this.customersService.allOrdersData(parseInt(id));
return data;
}
}
`
can anyone help pls
it must be some small issue..
first of all
```ts
your code
```
outputs
your code
second
export interface Request extends BaseRequest {
that's not how things works
you need to declare your interface as a module to mutate express request object
third
why are you trying to inject
the request object like that
that's like adding salt your salt because it is not salty enough
what you are doing is pointless
fourth
you dont need to build your own helper
nest already has one called handleRequest
handleRequest(error: Error, user, info, context, status) {
const req = context.switchToHttp().getRequest();
return super.handleRequest(error, user, info, context, status);
}
I'm pretty sure I've already mentioned this to you before: you're missing the @Req() to your request parameter of the OrderData method
The interface is fine, and actually my preferred approach over declaration merging. Especially because we aren't necessarily tied to express's Request interface
this is why syntax highlighting is important, missed that detail
Also, handleRequest is specific to AuthGuard from @nestjs/passport
Yeah i know 😂
I was reviewing it again..and i saw i missed it
the more you know i gues 🤷
Decorator
For the fourth one...i want to get all queries, params and body in one object that's why i made that guard
What i have to declare the interface as a module???
with what goal?
you can access the request object at anytime
srry i dont see the point of this
Well both ways work...
Actually i m new to nest js
And i m trying to understand my company code
They do it that way
i can understand that
So i m trying to understand it 😅
ah fair point, if you didn't write it
looks hela cursed if you ask me 😅
U know about objection js?
Well i can't do anything bout it..since i m in no position to tell them that..."why this loong approach"
😂
I have some doubts in it😅
cant argue with that
srry i just jumped in like that
but it looked like a ugly work around for something that didn't need a work around 😂