Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

sean shea
sean shea
22,104 Points

Wordpress Badges Plugin

Hello,

Recently I created a WordPress Plugin using ACF Blocks and Treehouse users API. I seem to be receiving an error for the original "newbie" badge. From what I can tell, the property "title" does not exist for that badge. What would be the best way to exclude it for only that badge?

Error: Uncaught TypeError: Cannot read property 'title' of undefined

const gatherBadges = () => {
    let badges = data.badges.reverse();
    // console.log(badges);
    for (i = 0; i < badges.length; i++ ) {
        let date = new Date(badges[i].earned_date);
        let newDate = date.toLocaleDateString();
        let courseTitle;

        if (badges[i].courses[0].title !="" || badges[i].courses[0].title != NULL || badges[i].courses[0].title != undefined) {
            courseTitle = badges[i].courses[0].title;
            console.log(courseTitle);
        } else {
            console.log('test');
        }

        let html = `
        <div class="treehouse-portfolio-badge">
            <div class="treehouse-portfolio-badge-content">
                <strong>Achievement</strong>
                <h3>${ badges[i].name }</h3>
                <h4>${ courseTitle }</h4>
                <div>
                    <strong>
                        Achieved
                    </strong>
                    <p>
                        ${ newDate }
                    </p>
                </div>
            </div> 
            <div class="treehouse-portfolio-badge-image">
                <img src="${badges[i].icon_url}" loading="lazy" /> 
            </div> 
        </div>`;

        jQuery(".treehouse-portfolio-badges").append(html);
    }
}

Demo: http://seansheadev.com/team-treehouse/

Full Code: https://github.com/cod3rShea/Treehouse-Badge-WordPress-Plugin

Thanks, Sean