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
3 Answers
Reset to default 8you 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
版权声明:本文标题:javascript - NestJS createParamDecorator return undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741246263a2364936.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论