TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
BD One
NA
11
580
how to put a declaration in a if-function?
Feb 23 2021 6:27 PM
hey community,
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
Reply
Answers (
3
)
how to add image to file type in typescript
How to read the value from an object in an array?