Tell us what’s happening:
i cant fetch data so i cant test the output, how to fix this. and please tell me what i did wrong, i dont see anyone encounter the same problem like this. thx
Access to fetch at ‘https://cdn.freecodecamp.org/curriculum/forum-latest/latest.json’ from origin ‘https://www.freecodecamp.org’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.Understand this error
cdn.freecodecamp.org/curriculum/forum-latest/latest.json:1 Failed to load resource: net::ERR_FAILEDUnderstand this error
frame.ts:339 TypeError: Failed to fetch
at _callee$ (about:srcdoc:372:44)
at tryCatch (about:srcdoc:238:1062)
at Generator. (about:srcdoc:238:3012)
at Generator.next (about:srcdoc:238:1699)
at asyncGeneratorStep (about:srcdoc:239:103)
at _next (about:srcdoc:240:194)
at about:srcdoc:240:364
at new Promise ()
at about:srcdoc:240:97
at _fetchData (about:srcdoc:385:21)
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */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://cdn.freecodecamp.org/curriculum/forum-latest';
const postsContainer = document.getElementById("posts-container");
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: 'Back-End Development', className: 'backend' }
};
function timeAgo(timestamp) {
const timeVar = Date.now() - (new Date(timestamp));
if(timeVar >= 86400000)
return `${Math.floor(timeVar / 86400000)}d ago`;
else if(timeVar >= 3600000)
return `${Math.floor(timeVar / 3600000)}h ago`;
else if(timeVar >= 60000)
return `${Math.floor(timeVar / 60000)}m ago`;
}
function viewCount(arg) {
return arg >= 1000 ? Math.floor(arg / 1000) + "k" : arg;
}
function forumCategory(id) {
if(allCategories.hasOwnProperty(id)) {
const chosen = allCategories[id];
return `<a href="${forumCategoryUrl}${chosen.className}/${id}" class="category ${chosen.className}">${chosen.category}</a>`
}
else {
return `<a href="${forumCategoryUrl}general/${id}" class="category general">General</a>`
}
}
function avatars(posters, users) {
console.log(posters, users);
let results = "";
for(const poster of posters) {
const user = users.find(usr => usr.id === poster.user_id);
results += `<img alt="${user.name}" src="${avatarUrl+user.avatar_template.replace("{size}", "30")}">`;
}
return results;
}
function showLatestPosts(arg) {
const results = "";
for(const topic of arg.topic_list.topics){
const {id, title, views, posts_count, slug, posters, category_id, bumped_at} = topic;
results += `<tr><td><a class="post-title" href="${forumTopicUrl+slug+id}">${title}</a>${forumCategory(category_id)}</td>`
results += `<td><div class="avatar-container">${avatars(posters, users)}</div></td>`
results += `<td>${posts_count-1}</td><td>${views-1}</td><td>${bumped_at}</td></tr>`
}
postsContainer.innerHTML = `${results}`;
}
async function fetchData() {
return fetch(forumLatest)
.then(response => response.json())
.then(data => showLatestPosts(data))
.catch(error => console.error(error));
}
fetchData();
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Challenge Information:
Build an fCC Forum Leaderboard - Build an fCC Forum Leaderboard



