const pictureEntity = updateUserDto?.picture
? await this.filesService.find(updateUserDto.picture)
: null;
if (pictureEntity) {
const pictureEntity.url = await this.filesService.getFileUrl(pictureEntity);
}
Is this the correct way to assign the value to pictureEntity? Basically if property picture is not defined, I'm not supposed to use the service find in filesService because typeORM will return the first value that it finds if property picture is null or undefined.
I was doing this:
if (updateUserDto?.picture) {
const pictureEntity = await this.filesService.find(updateUserDto.picture);
}
but TS would complain because I'm declaring a variable inside an If.
You could do:
const pictureEntity = updateUserDto?.picture && await this.filesService.find(updateUserDto.picture);
If updateUserDto
is null
or undefined
, pictureEntity
will be undefined
. Otherwise it will await
your other promise.
Edit:
There's also no need for a const
here:
if (pictureEntity) {
const pictureEntity.url = await this.filesService.getFileUrl(pictureEntity);
}
You don't use const
to create object properties.