Home > Back-end >  Google Scripts - getFrom() is not a function error
Google Scripts - getFrom() is not a function error

Time:06-16

I had this working before, without an issue, however ever since I put a filter in to remove all threads with more than 1 email it is now coming up with the not a function error. I remove the filter and it still comes up with the error, unsure what has caused this to completely break on me

    function extractEmails() {
  var htmlBody = getEmailHtml();
  var labelName = "auto-reply-incoming";
  // get all email threads that match label
  var receivedSearchQuery = "label:" labelName " -is:sent";
  var threads = GmailApp.search(receivedSearchQuery, 0, 500);
    threads.forEach ((t, i) => {
      let messages = t.getMessages();
      let name = messages.getFrom();
      let messageCount = t.getMessageCount();
      if (messageCount > 1) {
        label.removeFromThread(t);
      }
      if (messageCount <= 1) {
        message.reply("Hi "  name " \n"   "insert text here");
      }
    });
};

CodePudding user response:

Replace

let name = messages.getFrom();

by

let name = messages[0].getFrom();

The above because getFrom() is method from Class GmailMessage but messages is an Array.

Reference

CodePudding user response:

accidentally removed part of the script, fixed with the following code:

messages.forEach ((m, j) => {
      let name = m.getFrom();
      m.reply("Hi "  name " \n"   "insert text here");
});
  • Related