-1

I'm trying to create an app that imports contacts and puts them in a spreadsheet, but I keep getting this error: TypeError: Cannot call method "getAddress" of undefined

This even happends from the sample code from the gscript website:

var contacts = ContactsApp.getContactsByName('John Doe');
 var homeAddress = contacts[0].getAddresses(ContactsApp.Field.HOME_ADDRESS);
 Logger.log(homeAddress[0].getAddress());

I get the error:

TypeError: Cannot call method "getAddress" of undefined

also get the same thing when trying to import phone numbers even though it imports names and id's

2

The error message says that homeAddress[0] is undefined, which means that the method getAddresses(ContactsApp.Field.HOME_ADDRESS) returned empty array, which means the contact does not have a Home Address listed. Add a check for such situation:

var homeAddress = contacts[0].getAddresses(ContactsApp.Field.HOME_ADDRESS);
if (homeAddress.length) {
  Logger.log(homeAddress[0].getAddress());
}
else {
  Logger.log('No home address found')
}
  • What if there isnt any labor though? – Josh Carp Apr 24 '18 at 3:18
  • Label* I want to get any email thats there – Josh Carp Apr 24 '18 at 3:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.