admin管理员组文章数量:1025457
In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne
returns undefined?
@Controller('/customers')
export class CustomerController {
constructor (
@InjectRepository(Customer) private readonly repository: Repository<Customer>
) { }
@Get('/:id')
async getOne(@Param('id') id): Promise<Customer|undefined> {
return this.repository.findOne(id)
.then(result => {
if (typeof result === 'undefined') {
throw new NotFoundException();
}
return result;
});
}
}
Nestjs provides an integration with TypeORM and in the example repository is a TypeORM Repository
instance.
In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne
returns undefined?
@Controller('/customers')
export class CustomerController {
constructor (
@InjectRepository(Customer) private readonly repository: Repository<Customer>
) { }
@Get('/:id')
async getOne(@Param('id') id): Promise<Customer|undefined> {
return this.repository.findOne(id)
.then(result => {
if (typeof result === 'undefined') {
throw new NotFoundException();
}
return result;
});
}
}
Nestjs provides an integration with TypeORM and in the example repository is a TypeORM Repository
instance.
1 Answer
Reset to default 4You can write an interceptor that throws a NotFoundException
on undefined
:
@Injectable()
export class NotFoundInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { {
// next.handle() is an Observable of the controller's result value
return next.handle()
.pipe(tap(data => {
if (data === undefined) throw new NotFoundException();
}));
}
}
Then use the interceptor in your controller. You can use it per class or method:
// Apply the interceptor to *all* endpoints defined in this controller
@Controller('user')
@UseInterceptors(NotFoundInterceptor)
export class UserController {
or
// Apply the interceptor only to this endpoint
@Get()
@UseInterceptors(NotFoundInterceptor)
getUser() {
return Promise.resolve(undefined);
}
In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne
returns undefined?
@Controller('/customers')
export class CustomerController {
constructor (
@InjectRepository(Customer) private readonly repository: Repository<Customer>
) { }
@Get('/:id')
async getOne(@Param('id') id): Promise<Customer|undefined> {
return this.repository.findOne(id)
.then(result => {
if (typeof result === 'undefined') {
throw new NotFoundException();
}
return result;
});
}
}
Nestjs provides an integration with TypeORM and in the example repository is a TypeORM Repository
instance.
In order to avoid boilerplate code (checking for undefined in every controller, over and over again), how can I automatically return a 404 error when the promise in getOne
returns undefined?
@Controller('/customers')
export class CustomerController {
constructor (
@InjectRepository(Customer) private readonly repository: Repository<Customer>
) { }
@Get('/:id')
async getOne(@Param('id') id): Promise<Customer|undefined> {
return this.repository.findOne(id)
.then(result => {
if (typeof result === 'undefined') {
throw new NotFoundException();
}
return result;
});
}
}
Nestjs provides an integration with TypeORM and in the example repository is a TypeORM Repository
instance.
1 Answer
Reset to default 4You can write an interceptor that throws a NotFoundException
on undefined
:
@Injectable()
export class NotFoundInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> { {
// next.handle() is an Observable of the controller's result value
return next.handle()
.pipe(tap(data => {
if (data === undefined) throw new NotFoundException();
}));
}
}
Then use the interceptor in your controller. You can use it per class or method:
// Apply the interceptor to *all* endpoints defined in this controller
@Controller('user')
@UseInterceptors(NotFoundInterceptor)
export class UserController {
or
// Apply the interceptor only to this endpoint
@Get()
@UseInterceptors(NotFoundInterceptor)
getUser() {
return Promise.resolve(undefined);
}
版权声明:本文标题:javascript - How to return a 404 HTTP status code when promise resolve to undefined in Nest? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745622577a2159665.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论