# TypeError: Undefined on get all marketing emails

**URL:** <https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097>\
**Category:** APIs & Integrations\
**Tags:** email\
**Created:** [September 5, 2019, 8:17am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097 "2019-09-05T08:17:06Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 5, 2019, 8:17am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/1 "2019-09-05T08:17:06Z")

</div>

So I am trying to retrieve all my marketing emails with statistics. The json response works fine until it comes to my stats.

For example,

var no\_sent = object.stats.counter.sent;

Would throw a TypeError: Cannot read property “counter” from undefined.

When I looked at the response, it seems that the stats are not available and that field contains “undefined”. All other rows have a stats.counter.sent value.

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [September 5, 2019, 9:10am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/2 "2019-09-05T09:10:44Z")

</div>

Hello @kawyns1

Can you please write your piece of code that you are using to make API request?  
Thanks

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 5, 2019, 10:22am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/3 "2019-09-05T10:22:27Z")

</div>

```auto
 var go = true;
 var hasMore = false;
 var offset = 0;

 while (go)
 {
 var url_query = API_URL + "/marketing-emails/v1/emails/with-statistics?limit=300";
 if (hasMore)
 {
 url_query += "&offset=" + offset;
 }
 var response = UrlFetchApp.fetch(url_query, options).getContentText();
 response = JSON.parse(response);
 offset = response['offset'];
 if (offset > 0)
 {
 go = false;
 }
 Logger.log(response);
 response.objects.forEach(function(object) {

 var created = (object.hasOwnProperty('created')) ? object.created : 'NA';
 var createdById = (object.hasOwnProperty('createdById')) ? object.createdById : 'NA';
 var sent = object.stats.counter;!

```

@himanshurauthanFor some rows, the objects.stats = undefined

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [September 5, 2019, 10:59am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/4 "2019-09-05T10:59:53Z")

</div>

Hello @kawyns1

Can you write me the output of this line which you get

`Logger.log(response);`

I think

`response = JSON.parse(response);`

is not sufficient for getting into the response parameter. The json response contains one more index of “response” under which all objects are defined. Not sure if your code is traversing upto that level.

Thanks

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 5, 2019, 11:21am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/5 "2019-09-05T11:21:11Z")

</div>

Well I can’t show you the response for obvious reasons but what I can show is you is that I get this:

`undefined`

when I should be getting something like this:

`{deviceBreakdown={}, qualifierStats={}, counters={sent=0}, failedToLoad=false, ratios={bounceratio=0, unsubscribedratio=0, hardbounceratio=0, deliveredratio=0, contactslostratio=0, clickratio=0, softbounceratio=0, pendingratio=0, spamreportratio=0, notsentratio=0, clickthroughratio=0, openratio=0}}`

the “stats” part of the json is missing for a few of these records which in turn is missing my code

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [September 5, 2019, 12:17pm UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/6 "2019-09-05T12:17:18Z")

</div>

@kawyns1

I need the first two lines to see for

`response = JSON.parse(response);`

No need to send me full response. I am trying to configure one thing only and if you can sent me first 2-3 lines of code it would be helpful

Thanks

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 6, 2019, 4:43am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/7 "2019-09-06T04:43:30Z")

</div>

@himanshurauthan

I have erased all confidential parts and the some parts are labelled xxxxxxxxxxxxxx to remove the confidential info. As you can see, it is exactly the same as the json response given on the all marketing emails api documentation but except I am not getting stats.counter.sent data, instead it shows an undefined

`{total=86, offset=0, objects=[{emailBody={% content_attribute "email_body" %}{{ default_email_body }}{% end_content_attribute %}, absoluteUrl=http://confidential.hs-sites.com/-temporary-slug-xxxxxxxxxxxxxxxxxxxxxxxxxx, vidsIncluded=[], publishDate=1.567711080413E12, canSpamSettingsId=x.xxxxxxxxxxxxxE9,`

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [September 7, 2019, 6:32am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/8 "2019-09-07T06:32:52Z")

</div>

@kawyns1

Everything looks fine in your requests. Not sure what can be a cause here. May be the emails who do have stats data have not been sent any more from the CRM.  
@IsaacTakushi can you please here?

Thanks

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [September 19, 2019, 7:46pm UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/9 "2019-09-19T19:46:04Z")

</div>

Thanks for tagging me in, @himanshurauthan.

Hi, @kawyns1. Apologies for the delayed response; I have been out of the office.

To clarify, when you said:

_When I looked at the response, it seems that the stats are not available and that field contains “undefined”. All other rows have a stats.counter.sent value._

were you saying that one particular email’s `stats.counters.sent` field (note `counters` not `counter`) contains `undefined` while most or all other email objects you retrieved had `"sent":0` or some other valid number?

I’m testing [this endpoint](https://developers.hubspot.com/docs/methods/cms_email/get-the-statistics-for-a-marketing-email) for my own account and am not seeing any `undefined` values:

```auto
"stats": {
 "counters": {
 "sent": 1,
 "open": 1,
 "delivered": 1,
 "bounce": 0,
 "unsubscribed": 0,
 "click": 0,
 "dropped": 0,
 "selected": 1,
 "spamreport": 0,
 "suppressed": 0,
 "hardbounced": 0,
 "softbounced": 0,
 "pending": 0,
 "contactslost": 0,
 "notsent": 0
 }

```

If you’re consistently getting `undefined` for one or more of your emails, could you confirm the last four digits of your Hub ID and share the email ID with me?

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 23, 2019, 4:44am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/10 "2019-09-23T04:44:29Z")

</div>

Hi Isaac,

Thanks for the assistance. Yes, I fixed the counters issues. But now it seems I am having issues with getting the deviceBreakdown. Please see below:

This is the structure of my counters.sent:

`var sent = (object.hasOwnProperty('stats')) ? object.stats.counters.sent : 'NA' ;`

And for deviceBreakdown:

`var open_by_computer = (object.hasOwnProperty('stats')) ? objects.stats.deviceBreakdown.open_device_type.computer : 'NA' ;`

I seem to be getting undefined on my finding the open by and click by devices. I’m not sure what is causing this.

How can I e-mail you privately so I do not have my info here.

Thanks,

Kawyn

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [September 23, 2019, 1:54pm UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/11 "2019-09-23T13:54:02Z")

</div>

Hey, @kawyns1.

Happy to help. When you’re printing the response, you’re not seeing undefined values, though, right?

Feel free to direct message me. If that doesn’t work, I’ll share my email there.

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 23, 2019, 3:14pm UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/12 "2019-09-23T15:14:45Z")

</div>

Hi @IsaacTakushi ,

I do not see the undefined values. It just does not appear in the json. However, I see it in the spreadsheet. It is evident that the undefined values are the e-mails that have not been sent.

The question is how to stop it from breaking my code.

Regards,

Kawyn

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [September 24, 2019, 8:40pm UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/13 "2019-09-24T20:40:05Z")

</div>

Hi, @kawyns1.

I think I follow.

By spreadsheet, you’re referring to an [email performance export](https://knowledge.hubspot.com/articles/kcs_article/email/how-do-i-export-my-overall-email-performance-data), right? These exports show `0`s across the board for published but unsent emails.

When you retrieve the statistics for such an email via [this endpoint](https://developers.hubspot.com/docs/methods/cms_email/get-the-statistics-for-a-marketing-email), however, the `stats` object looks like this:

```auto
"stats": {
 "counters": {
 "sent": 0
 },
 "deviceBreakdown": {},
 "failedToLoad": false,
 "qualifierStats": {},
 "ratios": {
 "clickratio": 0,
 "clickthroughratio": 0,
 "deliveredratio": 0,
 "openratio": 0,
 "unsubscribedratio": 0,
 "spamreportratio": 0,
 "bounceratio": 0,
 "hardbounceratio": 0,
 "softbounceratio": 0,
 "contactslostratio": 0,
 "pendingratio": 0,
 "notsentratio": 0
 }
 }

```

While the `counters` object still includes a `"sent": 0` field, , `deviceBreakdown` is empty (`{}`), triggering an undefined error for you.

Could you add logic that only pulls `objects.stats.deviceBreakdown.open_device_type.computer` if `object.stats.counters.sent` is greater than `0`?

---

<div class="post-metadata">

**Author:** ![kawyns1](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kawyns1/32/27089_2.png) [@kawyns1](https://community.hubspot.com/u/kawyns1)\
**Post date:** [September 25, 2019, 2:05am UTC](https://community.hubspot.com/t/typeerror-undefined-on-get-all-marketing-emails/16097/14 "2019-09-25T02:05:43Z")

</div>

This seems like a solution, let me try than and get back to you.

Once again, thanks for you help and advice.

Kawyn
