From 9d177609f5adfa357363ba5356f51027a388f635 Mon Sep 17 00:00:00 2001 From: Paulo Castro Date: Tue, 27 Oct 2020 21:12:49 +0000 Subject: [PATCH] devices: Fix "TypeError: Cannot read property 'slug' of undefined" Change-type: patch --- doc/cli.markdown | 5 +++-- lib/commands/devices/index.ts | 24 ++++++++++++++---------- tests/commands/device/devices.spec.ts | 5 +++-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/doc/cli.markdown b/doc/cli.markdown index 6baa4940..bcef4b4b 100644 --- a/doc/cli.markdown +++ b/doc/cli.markdown @@ -513,8 +513,9 @@ You can filter the devices by application by using the `--application` option. The --json option is recommended when scripting the output of this command, because field names are less likely to change in JSON format and because it -better represents data types like arrays and empty strings. The 'jq' utility -may also be helpful in shell scripts (https://stedolan.github.io/jq/manual/). +better represents data types like arrays, empty strings and null values. +The 'jq' utility may be helpful for querying JSON fields in shell scripts +(https://stedolan.github.io/jq/manual/). Examples: diff --git a/lib/commands/devices/index.ts b/lib/commands/devices/index.ts index a7265d5a..e22d1a53 100644 --- a/lib/commands/devices/index.ts +++ b/lib/commands/devices/index.ts @@ -25,7 +25,8 @@ import type { Application } from 'balena-sdk'; interface ExtendedDevice extends DeviceWithDeviceType { dashboard_url?: string; - application_name?: string; + application_name?: string | null; + device_type?: string | null; } interface FlagsDef { @@ -45,8 +46,9 @@ export default class DevicesCmd extends Command { The --json option is recommended when scripting the output of this command, because field names are less likely to change in JSON format and because it - better represents data types like arrays and empty strings. The 'jq' utility - may also be helpful in shell scripts (https://stedolan.github.io/jq/manual/). + better represents data types like arrays, empty strings and null values. + The 'jq' utility may be helpful for querying JSON fields in shell scripts + (https://stedolan.github.io/jq/manual/). `; public static examples = [ '$ balena devices', @@ -97,14 +99,11 @@ export default class DevicesCmd extends Command { device.dashboard_url = balena.models.device.getDashboardUrl(device.uuid); const belongsToApplication = device.belongs_to__application as Application[]; - device.application_name = belongsToApplication?.[0] - ? belongsToApplication[0].app_name - : 'N/a'; + device.application_name = belongsToApplication?.[0]?.app_name || null; device.uuid = device.uuid.slice(0, 7); - // @ts-ignore - device.device_type = device.is_of__device_type[0].slug; + device.device_type = device.is_of__device_type?.[0]?.slug || null; return device; }); @@ -120,8 +119,8 @@ export default class DevicesCmd extends Command { 'os_version', 'dashboard_url', ]; + const _ = await import('lodash'); if (options.json) { - const _ = await import('lodash'); console.log( JSON.stringify( devices.map((device) => _.pick(device, fields)), @@ -130,7 +129,12 @@ export default class DevicesCmd extends Command { ), ); } else { - console.log(getVisuals().table.horizontal(devices, fields)); + console.log( + getVisuals().table.horizontal( + devices.map((dev) => _.mapValues(dev, (val) => val ?? 'N/a')), + fields, + ), + ); } } } diff --git a/tests/commands/device/devices.spec.ts b/tests/commands/device/devices.spec.ts index 20cb7925..c767f577 100644 --- a/tests/commands/device/devices.spec.ts +++ b/tests/commands/device/devices.spec.ts @@ -40,8 +40,9 @@ DESCRIPTION The --json option is recommended when scripting the output of this command, because field names are less likely to change in JSON format and because it - better represents data types like arrays and empty strings. The 'jq' utility - may also be helpful in shell scripts (https://stedolan.github.io/jq/manual/). + better represents data types like arrays, empty strings and null values. + The 'jq' utility may be helpful for querying JSON fields in shell scripts + (https://stedolan.github.io/jq/manual/). EXAMPLES $ balena devices