JavaScript SDK: Passing Device ID from "LIST DEVICES" to "GETATTRIBUTES" [problem]

Hi all,

I am trying to set up a Node.js server by following the JavaScript SDK. However, the challenge I am having is that I don’t want to manually insert the Device ID into the parameters. Is there a way to pass the Device ID from the LIST DEVICES function to the GETATTRIBUTES function or any of the others.
I understand that the LIST DEVICES function returns a Promise, since there maybe multiple devices. Thus, I will have to have a way to store and pass the list of Device ID’s into the other functions, as needed.
If there are any tutorials that walk you through the process, or if anyone has some code that I may be able to look at, I will greatly appreciate any help.
Thank you

My code so far:

var Particle = require('particle-api-js');

var particle = new Particle();

var userName = "";
var passWord = "";

particle.login({username: userName, password: passWord}).then(
  function(data){
    console.log('API call completed on promise resolve: ', data.body.access_token);
  },
  function(err) {
    console.log('API call completed on promise fail: ', err);
  }
);

var token; // from result of particle.login
var devicesPr = particle.listDevices({ auth: token });

devicesPr.then(
  function(devices){
    console.log('Devices: ', devices);
  },
  function(err) {
    console.log('List devices call failed: ', err);
  }
);


/*
var devicesPr = particle.getDevice({ deviceId: 'DEVICE_ID', auth: token });

devicesPr.then(
  function(data){
    console.log('Device attrs retrieved successfully:', data);
  },
  function(err) {
    console.log('API call failed: ', err);
  }
);
*/

Hello! JavaScript’s Promises are little confusing at first, but they make chaining asynchronous tasks quite expressive. You could use this to your advantage by chaining .thens. You could write something like:

var token;

particle.login({ username: 'bob', password: 'bob' })
  .then(function(response) {
    // Save in scope for use
    token = body.access_token;

    return particle.listDevices({ auth: token });
  })
  .then(function(deviceIds) {
    // Promise#all waits until all Particle#getDevice calls resolve
    return Promise.all(deviceIds.map(function(deviceId) {
      return particle.getDevice({
        auth: token,
        deviceId: deviceId,
      });
    }));
  })
  .then(function(devices) {
    // `devices` is an array of resolved Particle#getDevice responses
    console.log(devices);
  })
  .catch(function(error) {
    // Any API client error will get caught down here
    console.error(error);
  });

This isn’t tested, but it should get you working on the right path. Good luck!