#Guard Issue ..its not running

1 messages · Page 1 of 1 (latest)

sick star
#

`import { Request as BaseRequest } from 'express';

export interface Request extends BaseRequest {
/**

  • Get all inputs from the request object
    */
    all(): Record<string, any>;

/**

  • Get the current user from the request object
    */
    user: Record<string, any>;
    }
    `
#

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..

wanton bone
#

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);
  }
muted yarrow
muted yarrow
wanton bone
muted yarrow
sick star
#

I was reviewing it again..and i saw i missed it

wanton bone
sick star
#

Decorator

sick star
sick star
wanton bone
sick star
#

Actually i m new to nest js

#

And i m trying to understand my company code

#

They do it that way

wanton bone
sick star
#

So i m trying to understand it 😅

wanton bone
sick star
#

Well i can't do anything bout it..since i m in no position to tell them that..."why this loong approach"

#

😂

sick star
wanton bone
sick star
#

Yeah i know..maybe when i become senior developer there

#

Then i will fix it xD