I am mapping through an array of objects, some of the images are broken so I created a function to check the image which returns true or false and if false then use a placeholder image.
However I am getting an error and I think its because I am using a ternary operator inside the map function. Any ideas?
Function:
public renderProfile() {
// Grabs the array of objects
const profiles = this.state.profiles;
// Renders the selected profile
const renderProfiles = profiles.splice(0, this.props.postCount).map(profile => (
this.checkImageUrl(profile.imgUrl) ? profile.imgUrl : profile.imgUrl = 'https://via.placeholder.com/300x167.png?text=LINKEDIN';
<div key={shortid.generate()} className={styles.linkedInContainer}>
<DocumentCard
aria-label={profile.postContent}
onClickHref={profile.postUrl}
> { profile.imgUrl &&
<img className={styles.linkedInHeroImage } src={ profile.imgUrl } />
}
<DocumentCardTitle
title={profile.postContent}
shouldTruncate={true}
/>
<DocumentCardActivity
activity={`Likes: ${profile.likeCount}`}
people={[{ name: 'Read more on linkedIn', profileImageSrc: profile.imgUrl ? profile.imgUrl : 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAA' }]}
/>
</DocumentCard>
</div>
));
return renderProfiles;
}
This is braking it:
this.checkImageUrl(profile.imgUrl) ? profile.imgUrl : profile.imgUrl = 'https://via.placeholder.com/300x167.png?text=LINKEDIN';
[15:43:22] Error - [tsc] src/webparts/linkedIn/components/LinkedIn.tsx(143,157): error TS1005: ')' expected. [15:43:22] Error - [tsc] src/webparts/linkedIn/components/LinkedIn.tsx(163,5): error TS1128: Declaration or statement expected. [15:43:22] Error - [tsc] src/webparts/linkedIn/components/LinkedIn.tsx(163,6): error TS1128: Declaration or statement expected.
Check image function:
// Checks linkedIn images for broken ones
private checkImageUrl(url) {
var lastPart = url.substr(url.lastIndexOf('=') + 1);
if (lastPart === "image") {
return true;
} else {
return false;
}
}
https://codepen.io/bkdigital/pen/poojmbo?editors=1011 - This proves the check url function is working
urlexists in your checkImageUrl function above the string manipulation otherwise you will get nullexception if passed in null