I am making a pdf with information from a website. My problem is the image. If there is an image it works with the codes:
- const first_picture_url: string = product.pictures[0].url;
- const other_picture_urls: string[] = product.pictures.filter(
-
- (_: { url }, index: number) => index > 0).map((picture: { url }) => picture.url);
If there is no image, i get the Error
TypeError: Cannot read property 'url' of undefined.
i found out i could prevent this with
- if(product.pictures.length<1)
but if i try to put the first codes in this if-function it doesnt work.
My try:
- if(product.pictures.length>0){
- const first_picture_url: string = product.pictures[0].url;
- const other_picture_urls: string[] = product.pictures.filter(
-
- (_: { url }, index: number) => index > 0).map((picture: { url }) => picture.url);
- }
- else
- {
- const first_picture_url: string = String(product.airCoolingIntegrated);
- }
it doesnt work, i get the message "first_picture_url" is declared but its value is never read"
i just want these lines not be initiated if the length of the attribute is 0. (so no picture existing).
Can anyone help me with that maybe?
Greetings