Variable out of scope

Hello,

I’m using analysis script to a specific needs and I want to use the same variable in and out of scope, unfortunately this variable isn’t modified by the function and I didn’t understand why?

The script:

const Analysis = require('tago/analysis');
const Utils    = require('tago/utils');
const Device   = require('tago/device');

var x_value = 4;

// The function myAnalysis will run when you execute your analysis
function myAnalysis(context) {
  // reads the values from the environment and saves it in the variable env_vars
  const env_vars = Utils.env_to_obj(context.environment);
  if (!env_vars.device_token) return context.log('Missing device_token environment variable');

  const device = new Device(env_vars.device_token);

  // create the filter options to get the data from Tago
  const filter_x = {
    variable: 'accelerometer_x',
    query: 'last_item',
  };
  const filter_y = {
    variable: 'accelerometer_y',
    query: 'last_item',
  };
  const filter_z = {
    variable: 'accelerometer_z',
    query: 'last_item',
  };
  

  device.find(filter_x).then((result_array) => {
    // Check if the array is not empty
    if (!result_array[0]) return context.log('Empty Array');

    // query:last_item always returns only one value
    const variable = result_array[0].variable;
    x_value = result_array[0].value;
    const time = result_array[0].time;
    
    // print to the console at Tago
    context.log(`The last record of the ${variable} is ${x_value} and. It was inserted at ${time}`);

    const obj_x_to_save = {
      variable: 'x_value',
      value: x_value,
  };
    device.insert(obj_x_to_save).then(context.log('1 Successfully Inserted')).catch(error => context.log('Error when inserting:', error));
    context.log('x in scope = ', x_value);
  }).catch(context.log); //just prints the error to the console at Tago
  
  context.log('x out of scope  = ', x_value);

    const obj_to_save = {
      variable: 'battery_level',
      value: x_value * 3,
  };
    
    device.insert(obj_to_save).then(context.log('2 Successfully Inserted '));
}

// The analysis token in only necessary to run the analysis outside Tago
module.exports = new Analysis(myAnalysis, 'MY-ANALYSIS-TOKEN-HERE');

The console log:
image

I expected to see x out of scope = 2 but not at all…

Certainly something is wrong but i don’t no what thank you for your help.

The ‘device.find’ function is asynchronous, which means it will be executed in parallel with the rest of the code.

Read more on node’s async/await behavior on these links:


If you put the keyword async behind the function myAnalysis(context) { and the keyword await behind the device.find(filter_x).then((result_array) your code should work just fine.

This is a similar example so you can understand better what’s going on:

const Analysis = require('tago/analysis');
const Device = require('tago/device');

var x = 0;

async function myAnalysis(context, scope) {
  const device = new Device('MY_DEVICE_TOKEN');
  await device.find().then((result_array) => {
    x = 1;
  }); 
  context.log(x);
}

module.exports = new Analysis(myAnalysis, 'MY-ANALYSIS-TOKEN-HERE');

This example will output ‘1’ in the analysis console.

1 Like

Hello dear,

thank you for your reply and your explanation it’s totally clear.

Console log after modification:
image

1 Like