I'm having an issue while using database transaction to create a Page
record. Despite the fact that this.pagesService.create()
only returns Page
and will throw an error if something goes wrong, I keep receiving a
Variable 'createdPage' is used before being assigned
error. Therefore, the program should guarantee that createdPage is set if no exception is thrown. Why am I encountering this error?
@Post('')
async create(
@Body() body: PageCreateDto,
): Promise<Page> {
let createdPage: Page;
try {
await this.database.transaction(async trx => {
createdPage = await this.pagesService.create(body, trx);
});
} catch (error) {
throw new InternalServerErrorException('unable to create page');
}
return createdPage;
}