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 trialPatrick McDonough
11,113 PointsUnexpected Type error recentSearches.forEach is not a function.
I got the error forEach not a function
. Looks like my code is verbatim to what is in the video. I'm sure I'm missing something ...
var recentSearches = getRecentSearches();
recentSearches.forEach(function(searchString) {
appendListItem(recentSearchList, searchString);
});
Patrick McDonough
11,113 PointsThanks Qasim, I was using workspaces.
7 Answers
Victor Learned
6,976 PointsI just hit this same issue and I think it's a result of bad data in your localStorage when using the Treehouse workspace. I'm pretty sure this is a result of the bug shown in the video of passing the incorrect data to JSON.stringify at the 10:17 mark. If manually clear localStorage in the dev tools it immediately fixed this error.
Alan McClenaghan
Full Stack JavaScript Techdegree Graduate 56,501 PointsExcellent, thanks!
Adam Frank
2,981 PointsHey I ran into this problem as well. Even though my code was correct, 'recentSearches' was still returning a string, not an array.
I thought maybe this was because 'recentSearches' was unable to change type.
I manually changed the recentSearches value to an empty array.
What I did was in the js console typed
localStorage.setItem('recentSearches', []);
then
local.Storage.recentSearches
to verify that it was indeed now an array.
Then 'getRecentSearches' worked just fine. Hope this helps.
Kevin Snider
Front End Web Development Techdegree Student 25,275 PointsI was using Sublime for this project and had the same error.
I did what Victor Learned suggested and cleared the local storage in the chrome dev tools with:
localStorage.clear();
That fixed the problem.
Joel Kraft
Treehouse Guest TeacherHi Patrick! As Steven mentioned, .forEach()
is a method that exists on arrays, so if getRecentSearches()
does not return an array, you will see that error. I'm not sure how far along you are yet, but if your workspace is in the starting state for this workshop, getRecentSearches()
will be empty, so the error will be present. By following along with the video, you should see the error disappear once getRecentSearches()
is implemented (around the 5:36 mark).
If this doesn't solve your issue, it might be helpful to see what you have for getRecentSearches()
.
Steven Parker
231,272 PointsThe .forEach method is an extension for arrays. If getRecentSearches returned something other than an array, that might cause this error. You didn't include that part of the code, but I would certainly look there.
Next time, remember you can share everything by making a snapshot of your workspace and posting the link to it.
Patrick McKinney
13,151 PointsI also downloaded the files because workspaces was not loading (kept getting 403 errors in the console). When I attemtped to run the finished project, I got the recentSearches.forEach is not a function.
Maybe this issue is related to Node.js? It looks like Node.js is used to build the files. Maybe the project files have to be run through Node?
Joel Kraft
Treehouse Guest TeacherNode is just used to create a simple server that serves the files to your browser. It should also work to just drag index.html
into chrome or firefox (in this case your filesystem would be serving the files) though keep in mind this isn't a development best-practice.
I just downloaded and ran the finished project, and I'm not getting the error.
What browser and version are you using?
Patrick McKinney
13,151 PointsToday I tried to run the project through Bracket's live preview, and it worked. Yesterday I tried to just open the file from file explorer.
Perhaps this had something to do with the issue?
Qasim Hafeez
12,731 PointsQasim Hafeez
12,731 PointsAre you using workspaces or your own text editor? I'm using my own text editor and got the same error. I copied my code into workspaces and it worked perfectly. The instructions say that if you're going to use your own text editor to install something on the command line. I tried to install it but, was not able to.