Johny
Johny

Reputation: 39

How to return property while object get undefined

I got this error when trying to grab emails from Gmail inbox.

TypeError: Cannot read property "length" from undefined

I don't know if there is any other functions fit here better.

function getRelevantMessages()
{
  var threads = GmailApp.search("newer_than:30d AND label:payments",0,100);
  var messages=[];
  threads.forEach(function(thread)
                  {
                    messages.push(thread.getMessages()[0]);
                  });
  return messages;
}

function parseMessageData(messages)
{
  var records=[];
  for(var m=0;m<messages.length;m++)
  {
    var text = messages[m].getPlainBody();
// then regex and objects carry the returns

the code from pastebin https://pastebin.com/TRkEB6yM

Upvotes: 0

Views: 128

Answers (1)

Jescanellas
Jescanellas

Reputation: 2608

Since the original question was getting undefined when searching for emails, MiMi, Cooper and Tedinoz solved it in the comments. Posting it as an answer for further searches.

function getRelevantMessages() { 

   var threads = GmailApp.search("newer_than:30d",0,100); 
   var messages=[];
   threads.forEach(function(thread) { 
       messages.push(thread.getMessages()[0].getFrom()); 
   }); 

  Logger.log(messages); 
  return messages; 

}

Upvotes: 1

Related Questions