title: Facebook canvas: Uncaught TypeError: Cannot call method 'sort' of undefined
body: <p>I have a strange problem. My javascript code of multi-friend selector runs fine on my localhost but when I load it inside the canvas of my facebook app, it throws an error</p>
  
  <pre><code>Uncaught TypeError: Cannot call method 'sort' of undefined
  </code></pre>
  
  <p>at line 81 of all.js When I debug with the chrome developer tools on localhost, I can see the response object calling the sort method but in facebook app, the response object is the error object. Is there any thing wrong with my javascript? OR any specific task to do while loading in iframe on facebook?.</p>
  
  <p><strong>I have loaded the libraries in the following order:</strong></p>
  
  <pre><code>    &lt;script src="/static/interface/js/jquery.min.js"&gt;&lt;/script&gt; 
    &lt;script src="http://connect.facebook.net/en_US/all.js"&gt;&lt;/script&gt;
          &lt;script src="/static/interface/js/bootstrap.min.js"&gt;&lt;/script&gt;
          &lt;script src="/static/interface/js/jquery.facebook.multifriend.select.js"&gt;&lt;/script&gt; 
          &lt;link rel="stylesheet" href="/static/interface/css/jquery.facebook.multifriend.select.css"/&gt; 
  </code></pre>
  
  <p><strong>And here is the javascript code</strong></p>
  
  <pre><code>&lt;script&gt; 
  
  
      FB.init({appId: '460948667348013', cookie: true});
  
      FB.getLoginStatus(function(response) {
          if (response.session) {
            init();
          } else {
            // no user session available, someone you dont know
          }
      });
  
      function init() {
        FB.api('/me', function(response) {
            $("#username").html("&lt;img src='https://graph.facebook.com/" + response.id + "/picture'/&gt;&lt;div&gt;" + response.name + "&lt;/div&gt;");
  
  
  
            $("#jfmfs-container").jfmfs({ 
                max_selected: 15, 
                max_selected_message: "{0} of {1} selected",
                friend_fields: "id,name,last_name",
                pre_selected_friends: [1014025367],
                exclude_friends: [1211122344, 610526078],
                sorter: function(a, b) {
                  var x = a.last_name.toLowerCase();
                  var y = b.last_name.toLowerCase();
                  return ((x &lt; y) ? -1 : ((x &gt; y) ? 1 : 0));
                }
            });
            $("#jfmfs-container").bind("jfmfs.friendload.finished", function() { 
                window.console &amp;&amp; console.log("finished loading!"); 
            });
            $("#jfmfs-container").bind("jfmfs.selection.changed", function(e, data) { 
                window.console &amp;&amp; console.log("changed", data);
            });                     
  
            $("#logged-out-status").hide();
            $("#show-friends").show();
  
        });
      }              
  
  
  
      $("#show-friends").live("click", function() {
          var friendSelector = $("#jfmfs-container").data('jfmfs');             
          $("#selected-friends").html(friendSelector.getSelectedIds().join(', ')); 
  
  
  
      });                  
      function sendRequest() {
         var friendSelector = $("#jfmfs-container").data('jfmfs');
         var sendUIDs = friendSelector.getSelectedIds().join(', '); 
         // Use FB.ui to send the Request(s)
         FB.ui({method: 'apprequests',
           to: sendUIDs,
           title: 'My Great Invite',
           message: 'Check out this Awesome App!',
         }, callback);
       }
  
       function callback(response) {
          // alert('callback called');
         var friendSelector = $("#jfmfs-container").data('jfmfs');
         var sendUIDs = friendSelector.getSelectedIds().join(','); 
         var uids = sendUIDs.split(',');
         var query = '';
         for(i=0;i&lt;uids.length;i++){
          if(i==0){
              query =  query + 'to[' + i + ']=' + uids[i];
          }
          else{
          query =  query + '&amp;to[' + i + ']=' + uids[i];
          }
         }
         console.log(query);
         if(response){
          // alert('successful');
          window.location.assign("/?"+ query)
         }
         else{
          alert('failure');
         }
  
       }
  window.onload = function() {
        init();
      };
  
    &lt;/script&gt; 
  </code></pre>