title: Result of function returning undefined
body: <p>I'm trying to use a Service Account to populate a spreadsheet for end users.</p>
  
  <p>I am using this script to get the ChromeOS devices:</p>
  
  <pre><code>function listChromeOSDevices_(accessToken) {
    var devices = [];
    var pageToken;
    do {
      var params = {
        method: 'get',
        headers: {
          Authorization: 'Bearer ' + accessToken
        }
      };
      var url =
        'https://www.googleapis.com/admin/directory/v1/customer/my_customer/devices/chromeos?projection=full&amp;orderBy=status&amp;sortOrder=ascending&amp;maxResults=100';
      if (pageToken) {
        url += '&amp;pageToken=' + pageToken;
      }
      var urlResponse = UrlFetchApp.fetch(url, params);
      var jsonContent = JSON.parse(urlResponse.getContentText());
      devices = devices.concat(jsonContent.chromeosdevices);
      pageToken = jsonContent.nextPageToken;
    } while (pageToken);
    return devices;
  }
  </code></pre>
  
  <p>Then trying to do this with the results:</p>
  
  <pre><code>function main() {
    var userEmail = 'admin@email.com';
    var accessToken = getOAuthToken(userEmail);
    var chromeOSDevices = listChromeOSDevices_(accessToken);
    var date = new Date().toLocaleDateString();
    var outputTableHeaders = [
       'Date and Duration of Usage',
       'Asset ID',
       'Location',
       'User',
       'Admin Notes',
       'Firmware Version',
       'Last Synchronised',
       'MAC Address',
       'Device Model',
       'Serial Number',
       'OS Version',
       'Platform Version',
       'Device Status',
       'OU Path'
     ];
  
    var deviceResults = [outputTableHeaders].concat(
      chromeOSDevices.map(function(device) {
        device = fillInMissing_(device);
        device.activeTimeRanges = sortArrByDateElement_(
          device.activeTimeRanges,
          'asc'
        );
        return [
          dateConv_(
            device.activeTimeRanges[device.activeTimeRanges.length - 1].date
          ) +
            ' for ' +
            msToReadable_(
              device.activeTimeRanges[device.activeTimeRanges.length - 1]
                .activeTime
            ),
          device.annotatedAssetId,
          device.annotatedLocation,
          device.annotatedUser,
          device.notes,
          device.firmwareVersion,
          device.lastSync,
          device.macAddress,
          device.model,
          device.serialNumber,
          device.osVersion,
          device.platformVersion,
          device.status,
          device.orgUnitPath
        ];
      })
    );
  }
  </code></pre>
  
  <p>However, I am getting the error message <code>TypeError: Cannot call method "hasOwnProperty" of undefined.</code> The dateConv_ and msToReadable_ functions are helper functions - but they aren't the issue (have removed them and have the same problem).</p>
  
  <p>When logging out chromeOSDevices in the main code, it returns the devices as expected (as far as I can tell).</p>
  
  <p>However, when trying to use map it's almost as if the results have lost any additional properties. At a bit of a loss about this, as it looks fine to me!</p>
  
  <p>I was previously using the same method to output the results of <code>AdminDirectory.Chromeosdevices.list</code> (so I know that the map was working) but developed a need to use a service account for some of our users who do not have the required level of access to run the script, and that's where it has fallen over.</p>
  
  <p>Anyone able to figure out why? Because I've been sitting looking at it for an hour or two and have nothing...</p>
  
  <p><strong>Edit</strong><br>
  Removing the helper functions from the script I still get the error: <code>TypeError: Cannot read property "annotatedAssetId" from undefined.</code></p>
  
  <p><strong>Edit 2</strong><br>
  Logging it out like this:</p>
  
  <pre><code>  for (var index in chromeOSDevices) {
      Logger.log("INDEX: %s - The user is : %s",index,chromeOSDevices[index].annotatedUser)
        }
  </code></pre>
  
  <p>I get this response: <a href="https://pastebin.com/0uHJXfQ4" rel="nofollow noreferrer">Response @ pastebin</a></p>