title: How to handle undefined response from AJAX call on Autocomplete box?
body: <p>I have the following function:</p>
  
  <pre><code>function GetGames(request) {
      //Replace spaces with a '+'
      var url = request.term.replace(/\s/g,"+");
      return $.ajax({
          'url': "php/gamescript.php?search=" + url, 
          'dataType': 'json'
      }).then(function(data){
          return $.map(data.Game || [], function(v,i){
              return {
                  label: v.GameTitle + ' Game (' + v.ReleaseDate + ')',
                  value: v.GameTitle 
              }
          });
      })
  }  
  </code></pre>
  
  <p>and above it, code that handles the returned data:</p>
  
  <pre><code>function AutoComplete() {
      $(".searchbox").autocomplete({  
          source: function(request, response) {  
              $.when( GetGames(request) )
              .done(function(games) {
  
                  var term = request.term.toLowerCase();
                  var combine = games
                      .map((v,i) =&gt; {
                          //precompute such values if possible
                          return {
                              titleLowerCase: v.value.toLowerCase(),
                              termOffset: (v.value.toLowerCase().indexOf(term)+1) || Infinity,
                              //and keep a reference to the original data
                              data: v
                          }
                      })
                      .sort((a, b)=&gt;{
                          //sortOn(termOffset ASC, titleLowerCase ASC)
                          return (a.termOffset - b.termOffset) || 
                          (a.titleLowerCase !== b.titleLowerCase &amp;&amp; a.titleLowerCase &gt; b.titleLowerCase? 1: -1) || 0;
                      })
                      .map(v =&gt; v.data).slice(0, 15);
  
                  response(combine);   
              });
          }
      });
  }
  </code></pre>
  
  <p>My problem is that sometimes, the search on gamescript.php will return 'undefined', which will then cause this error: "Uncaught TypeError: Cannot read property 'toLowerCase' of undefined"</p>
  
  <p>What is a good way to handle this error? I tried doing a simple approach:</p>
  
  <pre><code> return {
       label: v.GameTitle + ' Game (' + v.ReleaseDate + ')',
       value: v.GameTitle || " "
   }
  </code></pre>
  
  <p>which at least makes the code <em>run</em>, but it ends up creating Autocomplete suggestions that say 'undefined' as the label. I also tried to write an if statement in the GetGames function, but ultimately I struggle with introducing new code without creating new errors with it.</p>
  
  <p>In case it's needed, the gamescript.php code is:</p>
  
  <pre><code>&lt;?php 
      $xml_string = file_get_contents("http://thegamesdb.net/api/GetGamesList.php?name=".$_REQUEST['search']);
      $xml = simplexml_load_string($xml_string);
      $json = json_encode($xml);
      $array = json_decode($json,TRUE);
      echo $json;
  ?&gt;
  </code></pre>
  
  <p>Edit: I've now tried this code: </p>
  
  <pre><code>function GetGames(request) {
      //Replace spaces with a '+'
      var url = request.term.replace(/\s/g,"+");
      return $.ajax({
          'url': "php/phpscript.php?search=" + url, 
          'dataType': 'json'
      }).then(function(data){
          return $.map(data.Game || [], function(v,i){
              if( typeof( v.GameTitle )!='undefined' ){
                  return {
                      label: v.GameTitle + ' Game (' + v.ReleaseDate + ')',
                      value: v.GameTitle 
                  }
              }
          });
      })
  }  
  </code></pre>
  
  <p>edit: I've since realized that if I add <code>alert(data.Game.GameTitle);</code> in the GetGames function, it successfully alerts the title of the game despite showing up as 'undefined' in the Autocomplete list. I have no idea why this is happening... Again, this error occurs when I search 'Hunger'. For other game queries I've tried, it works fine.</p>