Tell us what’s happening:
All of the tests pass except for #29 “If there is an error, your fetchData function should log the error to the console.”
The console is also logging: “Error fetching data: [Error: This is a test error]”
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/*const forumLatest =
'https://cdn.freecodecamp.org/curriculum/forum-latest/latest.json';
const forumTopicUrl = 'https://forum.freecodecamp.org/t/';
const forumCategoryUrl = 'https://forum.freecodecamp.org/c/';
const avatarUrl = 'https://sea1.discourse-cdn.com/freecodecamp';
const allCategories = {
299: { category: 'Career Advice', className: 'career' },
409: { category: 'Project Feedback', className: 'feedback' },
417: { category: 'freeCodeCamp Support', className: 'support' },
421: { category: 'JavaScript', className: 'javascript' },
423: { category: 'HTML - CSS', className: 'html-css' },
424: { category: 'Python', className: 'python' },
432: { category: 'You Can Do This!', className: 'motivation' },
560: { category: 'Backend Development', className: 'backend' }
};
const timeAgo = (timestamp) => {
const currentTime = new Date();
const then = new Date(timestamp);
const secondsPassed = Math.floor((currentTime - then) / 1000);
if (secondsPassed < 60) {
return `${secondsPassed}s ago`;
};
const minutesPassed = Math.floor(secondsPassed / 60);
if (minutesPassed < 60) {
return `${minutesPassed}m ago`;
};
const hoursPassed = Math.floor(minutesPassed / 60);
if (hoursPassed < 24) {
return `${hoursPassed}h ago`
};
const daysPassed = Math.floor(hoursPassed / 24);
return `${daysPassed}d ago`
};
const viewCount = (numOfViews) => {
if (numOfViews >= 1000) {
const viewsOver1k = Math.floor(numOfViews / 1000) + "k";
return viewsOver1k;
};
return numOfViews;
};
const forumCategory = (id) => {
if (id in allCategories) {
return `<a class="category ${allCategories[id].className}" href="${forumCategoryUrl}${allCategories[id].className}/${id}">${allCategories[id].category}</a>`
}
return `<a class="category general" href="${forumCategoryUrl}general/${id}">General</a>`
};
const avatars = (posters, users) => {
return posters.map((poster) => {
const user = users.find(user => poster.user_id === user.id);
if (user) {
let avatarSrc = user.avatar_template.replace('{size}', '30');
if (!avatarSrc.startsWith("http")) {
avatarSrc = `${avatarUrl}${avatarSrc.startsWith("/") ? "" : "/"}${avatarSrc}`;
}
return `<img src="${avatarSrc}" alt="${user.name}" />`;
}
return '';
}).join('');
};
const showLatestPosts = ({ users, topic_list }) => {
const { topics } = topic_list;
const rows = topics.map((topic) => {
const { id, title, views, posts_count, slug, posters, category_id, bumped_at } = topic;
return `
<tr>
<td>
<a class="post-title" href="${forumTopicUrl}${slug}/${id}">${title}</a>
${forumCategory(category_id)}
</td>
<td>
<div class="avatar-container">${avatars(posters, users)}</div>
</td>
<td>
${posts_count - 1}
</td>
<td>
${viewCount(views)}
</td>
<td>
${timeAgo(bumped_at)}
</td>
</tr>
`;
}).join('');
const postsContainer = document.getElementById("posts-container");
postsContainer.innerHTML = rows;
};
const fetchData = async () => {
try {
const res = await fetch(forumLatest);
const data = await res.json();
showLatestPosts(data);
console.log("Fetched data:", data)
} catch (error) {
console.error("Error fetching data:", error);
};
};
*/
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Challenge Information:
Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard
https://www.freecodecamp.org/learn/full-stack-developer/lab-fcc-forum-leaderboard/build-an-fcc-forum-leaderboard