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

Local Storage not saving searches. Uncaught TypeError: searches.indexOf is not a function

I believe my code is identical to Huston's, I can't seem to figure out why the localStotage object isn't being updated when a search is submitted. This is the error thrown:

index.html:47 Uncaught TypeError: searches.indexOf is not a function at saveSearchString (index.html:47) at HTMLFormElement.<anonymous> (index.html:86)

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <title>Search Bar Demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="stylesheets/page.css" />
  <link href='https://fonts.googleapis.com/css?family=Bitter:400,700' rel='stylesheet' type='text/css'>
</head>

<body>

  <div class="header">
    <h1 class="header-title">Search</h1>
    <form autocomplete="off" id="searchForm">
      <input class="text-input" type="text" id="searchBar">
      <button class="button" type="submit">Search</button>
    </form>
  </div>

  <div class="recent">
    <h2 class="recent-title">Recent Searches</h2>
    <button class="button" type="button" id="clearStorage">Clear Storage</button>
    <ul class="recent-list" id="recentSearchList"></ul>
  </div>

  <script type="text/javascript">
    'use strict';
    function supportsLocalStorage() {
      try {
        return 'localStorage' in window && window['localStorage'] !== null
      } catch (e) {
        return false;
      }
    }

    function getRecentSearches() {
      var searches = localStorage.getItem('recentSearches');
      if (searches) {
        return JSON.parse(searches);
      } else {
        return [];
      }
    }

    function saveSearchString(str) {
      var searches = getRecentSearches;
      if (!str || searches.indexOf(str) > -1) {
        return false;
      }
      searches.push(str);
      localStorage.setItem('recentSearches', JSON.stringify(searches));
      return true;
    }

    function removeSearches() {
      localStorage.removeItem('recentSearches');
    }

    // Create an li, given string contents, append to the supplied ul
    function appendListItem(listElement, string) {
      var listItemElement = document.createElement('LI');
      listItemElement.innerHTML = string;
      listElement.appendChild(listItemElement);
    }

    // Empty the contents of an element (ul)
    function clearList(listElement) {
      listElement.innerHTML = '';
    }
    if (supportsLocalStorage()) {
      window.onload = function () {
        var searchForm = document.getElementById('searchForm');
        var searchBar = document.getElementById('searchBar');
        var recentSearchList = document.getElementById('recentSearchList');
        var clearButton = document.getElementById('clearStorage');

        // Initialize display list
        var recentSearches = getRecentSearches();
        recentSearches.forEach(function (searchString) {
          appendListItem(recentSearchList, searchString);
        });

        // Set event handlers
        searchForm.addEventListener('submit', function (event) {
          var searchString = searchBar.value;
          if (saveSearchString(searchString)) {
            appendListItem(recentSearchList, searchString);
          }
        });

        clearButton.addEventListener('click', function (event) {
          removeSearches();
          clearList(recentSearchList);
        });
      };
    } 
  </script>
</body>

</html> ```

1 Answer

Hi Andrew. The searches variable in saveSearchString() has a typo in the declaration wherein you forgot to place the function parenthesis for the getRecentSearches() function call.

      var searches = getRecentSearches(); // <-- Note the addition of the brackets

Yep, sorry. I should review my code better. Sometimes you just need a second set of eyes! Thanks!