Already posted this on Slack, but here goes. I am learning Node and trying to use ParticleJS. When using the example code from https://docs.particle.io/reference/javascript/, it tries to list devices before the access token has been obtained. I’m still getting used to the asynchronicity of Node.
var Particle = require('particle-api-js');
var particle = new Particle();
var token;
particle.login({username: 'email', password:
'pass'}).then(
function(data) {
token = data.body.access_token;
console.log('Got token!');
},
function (err) {
console.log('Could not log in.', err);
}
);
var devicesPr = particle.listDevices({ auth: token });
devicesPr.then(
function(devices){
console.log('Devices: ', devices);
},
function(err) {
console.log('List devices call failed: ', err);
}
);
Output:
List devices call failed: { Error: HTTP error 400 from https://api.particle.io/v1/devices - The access token was not found
at /Users/nrobinson/test/node_modules/particle-api-js/lib/Agent.js:184:19
at Request.callback (/Users/nrobinson/test/node_modules/superagent/lib/node/index.js:631:3)
at /Users/nrobinson/test/node_modules/superagent/lib/node/index.js:795:18
at IncomingMessage.<anonymous> (/Users/nrobinson/test/node_modules/superagent/lib/node/parsers/json.js:16:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
statusCode: 400,
errorDescription: 'HTTP error 400 from https://api.particle.io/v1/devices - The access token was not found',
shortErrorDescription: 'The access token was not found',
error:
{ Error: Bad Request
at Request.callback (/Users/nrobinson/test/node_modules/superagent/lib/node/index.js:626:17)
at /Users/nrobinson/test/node_modules/superagent/lib/node/index.js:795:18
at IncomingMessage.<anonymous> (/Users/nrobinson/test/node_modules/superagent/lib/node/parsers/json.js:16:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
original: null,
response:
Response {
domain: null,
_events: {},
_eventsCount: 0,
_maxListeners: undefined,
res: [Object],
request: [Object],
req: [Object],
links: {},
text: '{"error":"invalid_request","error_description":"The access token was not found"}',
body: [Object],
files: undefined,
buffered: true,
headers: [Object],
header: [Object],
statusCode: 400,
status: 400,
statusType: 4,
info: false,
ok: false,
redirect: false,
clientError: true,
serverError: false,
error: [Object],
accepted: false,
noContent: false,
badRequest: true,
unauthorized: false,
notAcceptable: false,
forbidden: false,
notFound: false,
charset: 'utf-8',
type: 'application/json',
setEncoding: [Function: bound ],
redirects: [] },
status: 400 },
body:
{ error: 'invalid_request',
error_description: 'The access token was not found' } }
Got token!