I am working with nestjs and mongoose. is there a correct way to return the data or simplify this code? especially let response = ....
pets.controller.ts
.
.
//getPets
@Get('read')
async getPets(@Req() _req: Request, @Res() _res: Response) {
let response: HttpResponse = await this.petService.getAll()
.then(result => {
return {
code: HttpStatus.OK,
ok: true,
message: result,
}
}).catch(err => {
return {
code: HttpStatus.INTERNAL_SERVER_ERROR,
ok: true,
message: err,
}
});
return _res.status(response.code).json(response);
}
pets.service.ts
//READ ALL
async getAll(): Promise<PetInterface[]> {
return await this.petsModel.find();
}
or some good practices that are used?