title: TypeError: Cannot read property "length" from undefined
body: <p>I am trying to use the code below that i got from this link:</p>
  
  <p><a href="https://stackoverflow.com/questions/11857494/import-emails-that-fit-criteria-to-google-spreadsheet-using-apps-script/13509206#13509206">Import emails that fit criteria to Google Spreadsheet using apps script</a></p>
  
  <p>and it is giving me the error</p>
  
  <p>"TypeError: Cannot read property "length" from undefined."</p>
  
  <p>Can someone help?</p>
  
  <pre><code>   function getMessagesWithLabel() {
       var destArray = new Array();
        var threads = GmailApp.getUserLabelByName('Facebook').getThreads(1,10);
  
        for(var n in threads){
              var msg = threads[n].getMessages();
              var destArrayRow = new Array();
              destArrayRow.push('thread has '+threads[n].getMessageCount()+' messages');
                for(var m in msg){
                           destArrayRow.push(msg[m].getSubject());
                 }
        destArray.push(destArrayRow);           
              }
      Logger.log(destArray);
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sh = ss.getActiveSheet();
      if(ss.getLastRow()==0){sh.getRange(1,1).setValue('getMessagesWithLabel() RESULTS')};
      sh.getRange(ss.getLastRow()+1,1,destArray.length,destArray[0].length).setValues(destArray)
      }
  </code></pre>