Scope.find variable

Hi community,

We have an action, which triggers an analytics task.

this search works fine,

  const { location } = scope.find(x => x.variable === 'location');
  context.log('location lat:' + location.coordinates[1]);

but not, for example, the next one …

const { battery } = scope.find(x => x.variable === 'batv');
context.log('battery json ' + JSON.stringify(battery));

This is the output scope on analytics …

{“id”:“5f7f649008c9ff00278ccb37”,“created_at”:“2020-10-08T19:12:16.947Z”,“time”:“2020-10-08T19:12:16.947Z”,“bucket”:“5f7f3932aaf8b000278703a2”,“variable”:“alarm_status”,“origin”:“5f7f3932aaf8b000278703a1”,“serie”:“1602184336921”,“value”:false},
{“id”:“5f7f649008c9ff00278ccb38”,“created_at”:“2020-10-08T19:12:16.947Z”,“time”:“2020-10-08T19:12:16.947Z”,“bucket”:“5f7f3932aaf8b000278703a2”,“variable”:“batv”,“origin”:“5f7f3932aaf8b000278703a1”,“serie”:“1602184336921”,“value”:3.993},
{“id”:“5f7f649008c9ff00278ccb3e”,“created_at”:“2020-10-08T19:12:16.947Z”,“time”:“2020-10-08T19:12:16.947Z”,“bucket”:“5f7f3932aaf8b000278703a2”,“variable”:“location”,“origin”:“5f7f3932aaf8b000278703a1”,“serie”:“1602184336921”,“location”:{“type”:“Point”,“coordinates”:[2.494605,42.179084]},“value”:“42.179084,2.494605”}]

Best Regards,
Jordi.

hi @jordi,

Your “object destructuring” is wrong.

You can’t use “{ battery }”, because the object has only variable, value, time, origin and etc…

To use the value from battery you can use as:

// without object destructuring
const battery = scope.find(x => x.variable === ‘batv’);
context.log(battery.value); // printing value of batv variable

or

// with object destructuring
const { value } = scope.find(x => x.variable === ‘batv’);
context.log(value); // printing value of batv variable

See here more details about “object destructuring”: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
I hope it helps you :wink:

Thx,
Fabiano Eger

Perfect @eger , thanks … my mistake !