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 trialnauman rafique
3,292 PointsIn case someone having hard time with the error: "Console throws error: recentSearches.forEach is not a function". Try:
I was having following error: "Console throws error: recentSearches.forEach is not a function." Here is my solution with a little bit of explanation:
The error comes from the window.load function where we create variable 'recentSearches' The reason is, we don't have an array at the load. so if we set it like this:
var recentSearches = getRecentSearches();
console.log(typeof recentSearches);
// Here we will checked if we have recentSearches, then we go further with forEach
// if we don;t have recentSearches, do nothing
if (recentSearches) {
recentSearches.forEach(function (searchString) {
appendListItem(recentSearchList, searchString);
});
}
Then in saveSearchString function: function saveSearchString(str) {
// Here we are looking if we are getting anything from getRecentSearches,
// if there is nothing we will set an empty array
const searches = getRecentSearches() || [];
if (!str || searches.indexOf(str) > -1) {
return false;
}
localStorage.setItem('recentSearches', JSON.stringify(searches));
console.log(searches);
return true;
}
Hope it helps, if there is any issue, attach your code and we will try figure out together ;)