On Mar 13, 2015, at 7:14 PM, Marc Phillips wrote:
> I'm converting over some automation apps from perl to node. I've run into
> what I'm sure is me making incorrect assumptions.
>
> I have the following function. I load up a json config file that defines a
> list of clusters and nodes (F5 ha pairs), then go out and query the restapi
> on each nodes in a cluster to detemine their state.
> The problem I'm having is the rest call seems to only happen for the last
> item in the list.
>
> Here's my rest config:
>
> var client = require('node-rest-client').Client;
> client = new client(options_auth);
This is confusing. You are reusing the same variable for two different
purposes. For clarity, use two variables:
var Client = require('node-rest-client').Client;
var client = new Client(options_auth);
Now Client (uppercase C) is the class, and client (lowercase c) is the instance
of that class, as per usual JavaScript conventions.
> function getActive() {
> var clusters = require('../etc/clusters.json')
> for (var cluster in clusters) {
> console.log(cluster+':')
> clusterlist += " "+cluster;
> for (var index in clusters[cluster]) {
> for (var node in clusters[cluster][index]) {
>
> client.get("https://"+clusters[cluster][index][node]+"/mgmt/tm/cm/device",
> function(data, response){
> state = data["items"][1]["failoverState"];
You haven't written "var state", so you've made "state" a global variable.
Globals should be avoided.
> console.log(state);
Here you are logging the state after it has been assigned. This should be
printing the correct value, for each run through the loop.
> });
> console.log("\t"+node.yellow+':
> '+clusters[cluster][index][node].magenta+" state: "+state);
Here you are logging the state before it has been assigned. (This console.log
is outside of client.get's callback.) So you might get an undefined state, or
you might get the state of the last run. Neither of these is likely to be what
you want.
> }
> }
> }
> }
>
> The client.get seems to happen asychronously, and after some debugging only
> occurs to the against the last node in the list (I presently have 4 nodes
> defined in the clusters.json).
>
> I'm sure I'm doing this horribly wrong.
You have a function called getActive, and it gets data asynchronously (using
client.get) but it doesn't take any parameters. I assume the function is
supposed to get a list of active nodes, and then return that information to the
caller somehow. The usual way to return such information would be to have a
callback parameter, and for the function to call that callback once the answer
has been retrieved or computed.
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/D5760E3D-DC3B-4E8B-98A8-977D14D03E61%40ryandesign.com.
For more options, visit https://groups.google.com/d/optout.