Cannot read property ‘getThreads’ of null

gmailgoogle-apps-script

I have this code, and I'm not getting why the getThreads property is not being read.

Is getThreads() deprecated or so?

function cleanUp() {  
var delayDays = 2 // Enter # of days before messages are moved to trash   
var maxDate = new Date(); 
maxDate.setDate(maxDate.getDate()-delayDays);    
var label = GmailApp.getUserLabelByName("2dehands");  
var threads = label.getThreads() || [];  
for (var i = 0; i < threads.length; i++) {  
  if (threads[i].getLastMessageDate()<maxDate)
  {  
    threads[i].moveToTrash();
  } 
} 
}

TypeError: Cannot read property 'getThreads' of null
cleanUp @ Code.gs:6

Best Answer

The error means that the value/object referenced by label is null. Check if there is a label named 2dehands in the user Gmail mailbox.