title: The collection has not been initialized - Sharepoint Javascript
body: <p>I'm getting the following error when attempting to get an enumerator for a collection of lists: "Uncaught Error: The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."</p>
  
  <p>It happens on the line <code>var listEnumerator = lists.getEnumerator();</code> it seems to me that there is an issue in my attempt to load lists into the client object with <code>context.load(lists);</code> </p>
  
  <p>Here's the portion of my code that's causing the problem. I've marked the place just before the error is thrown.</p>
  
  <pre><code>//____________________________Required function for accessing the host site's info.___________________________________
  function getQueryStringParameter(param) {
  var params = document.URL.split("?")[1].split("&amp;");    
  for (var i = 0; i &lt; params.length; i = i + 1) {
      var singleParam = params[i].split("=");
      if (singleParam[0] == param) {
          return singleParam[1];
  }   
  }
  }
  
  //____________________________Begin checking for list_________________________
  function checkForList(listToFind, typeOfListToCreateIfTheListIsMissing)
  {
      var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
      var hostcontext = new SP.AppContextSite(context, hostUrl);
      var hostweb = hostcontext.get_web();
  
      var lists = hostweb.get_lists();
      context.load(lists);
      context.executeQueryAsync(checkIfListExistsUsingEnumerator(listToFind, lists, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed);
  }
  //Failed to get lists for some reason
  function onQueryFailed(sender, args) {  
      alert('We failed to retrieve lists. \n' + args.get_message() + '\n' + args.get_stackTrace());  
  }
  
  //____________________________Does list exist?____________________________
  function checkIfListExistsUsingEnumerator(listToFind, lists, hostweb, typeOfList)
  {   
      var listExists = false; 
      //!!!!!!!!!!!!!!! ERROR HERE !!!!!!!!!!!!!!!!       
      var listEnumerator = lists.getEnumerator();
      var title;
      while (listEnumerator.moveNext()) 
      {
          title = listEnumerator.get_current().get_title();  
  
          if (title == listToFind)
          {  
              listExists = true;              
          }  
      }
  
      if (!listExists) 
      {
          alert("It appears that a required list does not already exist. \nClick ok, and we'll automatically create one for you.");
          //Create a new list
          createList(listToFind, hostweb, typeOfList); 
      }
      else if (listExists)
      {
          //Do nothing.
      }         
  }
  
  //____________________________If it doesn't, create one on the local site____________________________
  function createList(nameOfNewList, hostweb, typeOfList) {
      var listCreationInfo = new SP.ListCreationInformation();
      listCreationInfo.set_title(nameOfNewList);
  
      if (typeOfList === "events")
      {
          listCreationInfo.set_templateType(SP.ListTemplateType.events);
      }
      else if (typeOfList === "contacts")
      {
          listCreationInfo.set_templateType(SP.ListTemplateType.contacts);
      }
      var lists = hostweb.get_lists();
      var newList = lists.add(listCreationInfo);
      context.load(newList);
      context.executeQueryAsync(onListCreationSuccess, onListCreationFail);
  }
  
  function onListCreationSuccess() {
      alert('List created successfully!');
  }
  
  function onListCreationFail(sender, args) {
      alert('Failed to create the list. ' + args.get_message());
  }
  </code></pre>
  
  <p>I've looked at this question <a href="https://stackoverflow.com/questions/22607974/sharepoint-javascript-collection-not-initialized-error">sharepoint javascript collection not initialized error</a> which seems to be fairly similar to mine, but I'm having trouble implementing the solution provided there, making me think my error may be have a different cause.</p>
  
  <p>I've also tried querying for the lists inside of the function that is throwing the error, but that doesn't seem to solve anything.</p>
  
  <p>For a little background, these functions are attempting to read all lists from the app's host site, check to see if a specified list exists, and create a list if no matching list exists. If there's a better way of doing that than what I'm attempting, I'd be open to that too.</p>
  
  <p>Any pointers?</p>
  
  <hr>
  
  <p>Some things I've tried that don't seem to work:</p>
  
  <p>Changing the Asynchronous query</p>
  
  <pre><code>context.executeQueryAsync(checkIfListExists(listToFind, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed);
  </code></pre>
  
  <p>to a Synchronous one.</p>
  
  <pre><code>context.executeQuery(checkIfListExists(listToFind, hostweb, typeOfListToCreateIfTheListIsMissing), onQueryFailed); 
  </code></pre>