I'm trying to use database transaction to create a Page
record however I'm getting Variable 'createdPage' is used before being assigned
even though this.pagesService.create()
only returns Page
and it will throw error if something goes wrong so program can be sure that createdPage is set if no exception is thrown. So why I'm getting 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;
}