The following script for the Gmail API (script.google.com) works without any problems when I run it manually, but when it's triggered by Google using a time-based trigger it fails.
function noSend(e) {
var drafts = GmailApp.getDrafts();
var callback = function(draft) {
function isMyEx(draft) {
var recipient = draft.getMessage().getTo();
var isTheirEmail = /nameofmyex/.test(recipient);
return isTheirEmail; };
if(isMyEx(draft)) {
draft.update('', '', '');
draft.deleteDraft();
} else { } };
drafts.forEach(callback); return;
};
Question: How can I debug this when the error doesn't occur when I run the script manually?
I.e. adding Logger.log()
doesn't help, because the only way I get to see its output is when I run the script manually -- but the script works without any problems when I run it manually.
The error in question, as far as I can tell from Google's website (I don't understand the layout) is:
TypeError: Function getMessage in Object [object Object] was not found
which doesn't make any sense to me, sine the only time the getMessage
method is called is by draft
in the function isMyEx
, which shouldn't have Object
as its prototype, but instead should have GmailDraft
as its prototype, which most definitely does have a getMessage
method.
My only guess so far is that it has something to do with function hoisting, whose rules I don't understand very well. But I've already played around with several ways of ordering the functions and where to define them -- all of them worked when I ran the script manually. Again, this failure is occurring only when the script is triggered automatically by Google, which confuses me to no end.
Related questions: Gmail API message.list from trigger
Function works only when launched manually (not when triggered) in Google Apps Script Form