admin管理员组

文章数量:1279210

I'm using Nest version ^6.7.2

I'm trying to create a createParamDecorator that gets the req.user value from a request.

Inside the createParamDecorator, the req.user has a value, however when I try to get the value in a controller by using the decorator the value is undefined.

const AuthSession = createParamDecorator((data, req) => {
  console.log(req.user); // session data
  return req.user;
});
Controller()
export default class AuthController {
  @Get("/token/ping")
  @UseGuards(AuthGuard("jwt"))
  tokenPing(@AuthSession() session: Session) {
    console.log(session); // undefined
    return session;
  }
}

Edit: I just tried updating to nestjs v7 and I'm having the same issue

import { createParamDecorator, ExecutionContext } from "@nestjs/mon";

const AuthSession = createParamDecorator((data: any, ctx: ExecutionContext) => {
  return { message: "asdf" };
});

export default AuthSession;
@Controller()
export default class AuthController {
  @Get("/token/ping")
  @UseGuards(AuthGuard("jwt"))
  tokenPing(@AuthSession() session: Session) {
    console.log(session); // undefined
    return session;
  }
}

I'm using Nest version ^6.7.2

I'm trying to create a createParamDecorator that gets the req.user value from a request.

Inside the createParamDecorator, the req.user has a value, however when I try to get the value in a controller by using the decorator the value is undefined.

const AuthSession = createParamDecorator((data, req) => {
  console.log(req.user); // session data
  return req.user;
});
Controller()
export default class AuthController {
  @Get("/token/ping")
  @UseGuards(AuthGuard("jwt"))
  tokenPing(@AuthSession() session: Session) {
    console.log(session); // undefined
    return session;
  }
}

Edit: I just tried updating to nestjs v7 and I'm having the same issue

import { createParamDecorator, ExecutionContext } from "@nestjs/mon";

const AuthSession = createParamDecorator((data: any, ctx: ExecutionContext) => {
  return { message: "asdf" };
});

export default AuthSession;
@Controller()
export default class AuthController {
  @Get("/token/ping")
  @UseGuards(AuthGuard("jwt"))
  tokenPing(@AuthSession() session: Session) {
    console.log(session); // undefined
    return session;
  }
}
Share Improve this question edited Nov 26, 2020 at 19:43 user1991252 asked Nov 26, 2020 at 19:22 user1991252user1991252 1362 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

you can get the information firs from ExecutionContext:

    import { createParamDecorator, ExecutionContext } from '@nestjs/mon';

export const User = createParamDecorator(
  (data: unknown, ctx: ExecutionContext) => {
    const request = ctx.switchToHttp().getRequest();
    return request.user;
  },
);

check the example in the doc : Custom decorator

I figured out what the issue was. I had a custom validation PipeTransform that returned undefined if the ArgumentMetadata.type was neither "body" nor "param". So now I just return the first argument of the validator's transform method (the input) if the ArgumentMetadata.type is neither "body" nor "param" and that fixed the problem.

Note, to those facing similar issues, I missed out the first data argument which was throwing me an error.

createParamDecorator((ctx: ExecutionContext) would result in an undefined context, whereas createParamDecorator((data: unknown, ctx: ExecutionContext) works.

本文标签: javascriptNestJS createParamDecorator return undefinedStack Overflow