Particle.getVariable not returning (data.result) js

Hi, I have a argon that measures temperature and both publishes it as an event and as ‘Particle.variable’.
I have set up a webserver where I can call particle.getVariable and I can read the entire data, but I get undefined when trying to console.log(data.result).
From the API documentation: “If getting the variable succeeds, data.result is the value of the variable on the Particle device.”

Can you provide the code snippet you are seeing this with?

From the sparse context of your question I’d be inclinde to assume that you console.log() before the promis has returned any valid result.

I am using the example from the API where I filled in the neccessary parameters, and it prints the entire data, but I am just interested in the data.result.

particle.getVariable({ deviceId: 'DEVICE_ID', name: 'temp', auth: token }).then(function(data) {
  //console.log('Device variable retrieved successfully:', data);
  console.log(data.result);
}, function(err) {
  console.log('An error occurred while getting attrs:', err);
});

Output:

undefined

Granted, the docs are a bit misleading (possibly outdated) here, but since you said

So something like this

Device variable retrieved successfully: { body:
   { cmd: 'VarReturn',
     name: 'temp',
     result: 878,
     coreInfo:
      { last_app: '',
        last_heard: '2019-05-13T11:25:41.018Z',
        connected: true,
        last_handshake_at: '2019-05-13T11:25:38.281Z',
        deviceID: '<--yourDeviceID-->',
        product_id: 6 } },
  statusCode: 200 }

And by looking at that you could deduced that the info you are interested in may be found via

  console.log(data.body.result);
1 Like

Thanks, I have close to no experience when it comes to JavaScript, hence I’m trying to learn it through other examples and the Particle API. I’ve always dreamed about being able to create IoT-projects and this is a good start :wink:

1 Like

I’ve corrected the docs.
It now reads

2 Likes