TypeError: Cannot find function hasNext in object

2019-08-15 04:26发布

问题:

I'm trying to save an email in to a particular drive folder however getting error message

TypeError: Cannot find function hasNext in object

Any idea how I can overcome this.

function saveGmailToDrive() { 
    var gmailLabels  = "SaveToDrivetest";  
    var driveFolder  = "Emails saved by " + Session.getActiveUser().getEmail()

    /* Gmail Label that contains the queue */
    var label = GmailApp.getUserLabelByName(gmailLabels) ? 
                    GmailApp.getUserLabelByName(gmailLabels) :
                    GmailApp.createLabel(gmailLabels);

    var threads = GmailApp.search("in:" + gmailLabels, 0, 5);  

    if (threads.length > 0) {    
        /* Google Drive folder where the Files would be saved */
        var folders = DriveApp.getFolderById("0B9Ug3fXvrl5HeGRddddMcE93bkE3Q1k");
        var folder = folders.hasNext() ?
                         folders.next() : 
                         DriveApp.createFolder(driveFolder);
    }

回答1:

In general: if the noun after "get" is in plural form, you get an iterator to be used with hasNext and next methods. If that noun is in singular form, you get a single object. Examples:

  • getFileById returns one File
  • getFiles returns an iterator
  • getFilesByName returns an iterator
  • getFilesByType returns an iterator
  • getFolderById returns one Folder
  • getFolders returns an iterator
  • getFoldersByName returns an iterator