Node.js Nest.js中的嵌套拦截器
如何在Nest.js中实现嵌套拦截器 我有两个拦截器:Node.js Nest.js中的嵌套拦截器,node.js,typescript,nestjs,Node.js,Typescript,Nestjs,如何在Nest.js中实现嵌套拦截器 我有两个拦截器:UsersInterceptor和postsintterceptor 用户接收器: @Injectable() export class UsersInterceptor<T> implements NestInterceptor<T, Response<T>> { intercept(context: ExecutionContext, next: CallHandler): Observable&l
UsersInterceptor
和postsintterceptor
用户接收器:
@Injectable()
export class UsersInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
email: data.email,
createdAt: data.createdAt
})));
}
}
@Injectable()
export class PostsInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
title: data.title,
content: data.content,
user: {}, // I want the UsersInterceptor result here. I have the user's data in data.user
createdAt: data.createdAt
})));
}
}
@Injectable()
导出类UsersInInterceptor实现NestInterceptor{
拦截(上下文:ExecutionContext,下一步:CallHandler):可观察{
返回next.handle().pipe(映射(数据=>({
id:数据。\u id,
电子邮件:data.email,
createdAt:data.createdAt
})));
}
}
PostsInterceptor:
@Injectable()
export class UsersInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
email: data.email,
createdAt: data.createdAt
})));
}
}
@Injectable()
export class PostsInterceptor<T> implements NestInterceptor<T, Response<T>> {
intercept(context: ExecutionContext, next: CallHandler): Observable<Response<T>> {
return next.handle().pipe(map(data => ({
id: data._id,
title: data.title,
content: data.content,
user: {}, // I want the UsersInterceptor result here. I have the user's data in data.user
createdAt: data.createdAt
})));
}
}
@Injectable()
导出类PostsInterceptor实现NestInterceptor{
拦截(上下文:ExecutionContext,下一步:CallHandler):可观察{
返回next.handle().pipe(映射(数据=>({
id:数据。\u id,
标题:data.title,
内容:data.content,
user:{},//我想在这里得到UsersInterceptor结果。我在data.user中有用户的数据
createdAt:data.createdAt
})));
}
}
现在,当我使用
UserInterceptor
时,我想将UserInterceptor
的结果放入PostsInterceptor
中的user对象中。拦截器按绑定顺序处理传入请求,按绑定的相反顺序处理传出响应。这意味着,如果您像@UseInterceptors(PostInterceptor,UserInterceptor)那样绑定拦截器
那么您应该能够在PostInterceptor
中看到UserInterceptor
的返回,我如何告诉PostsInterceptor
从UsersInterceptor
加载用户
对象?它应该在返回的数据中。只要UserInterceptor
将其添加到返回的数据中,它就会存在