devices: Fix "TypeError: Cannot read property 'slug' of undefined"

Change-type: patch
This commit is contained in:
Paulo Castro 2020-10-27 21:12:49 +00:00
parent 826b0659d6
commit 9d177609f5
3 changed files with 20 additions and 14 deletions

View File

@ -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:

View File

@ -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,
),
);
}
}
}

View File

@ -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