Node js code question for devs

I'm working on a node.js app for Harmony updates. Running into a problem trying to update a global variable (activityXref). The idea is that I create a global array variable that contains a cross-reference between Harmony activity names and Hubitat switch names. Then I call a function to convert the HE names to switch IDs. The logic works but I think I'm running into some kind of scope issue with the variable, or maybe it's processing things in the wrong order? Here's the code:

var activityXref = //create an array variable to associate activity names from Harmony to switch names from Hubitat
					[
						['Fire TV','Master Bedroom Fire TV'],
						['Broadcast TV','Master Bedroom Broadcast TV'],
						['Cast','Master Bedroom Cast'],
						['Blu Ray','Master Bedroom Blu Ray']
					]
					
					
getSwitchIds();

function getSwitchIds () {
	var options = {
		uri: makerLink,
		method: 'GET',
		json: true
		}
	request(options, function(error, response, body) {
		if (!error && response.statusCode == 200) {
			var i
			var j 
			for(i = 0; i < body.length; i++) {
				for (j = 0; j < activityXref.length; j++) {
						if(body[i].label == activityXref[j][1]) {
							activityXref[j][1] = body[i].id
					}
				}			
			}
		}
		console.log('1 - ' + activityXref)
	})
	console.log('2 - ' + activityXref)
}

Here's the console output:

The line that shows "1 - Fire TV, 200,Broadcast TV,201" etc is the desired result after converting the switch names to switch IDs. However, once I step out of the request() function, those results appear to be lost and the variable has the values of the initial declaration.

What am I doing wrong? I want activityXref to retain the switch ID numbers so I can use them further down the line.

Not sure how many people will reply to this given node.js is nothing to do with Hubitat itself, you'd probably do better posting on a node.js forum!

That said, I can point you in the right direction ....

node.js is primarily designed to be event driven, so "request" takes a callback function, which it "calls" once the request function is complete (i.e. results, or an error, have been returned).

Your code is doing exactly what it should do, i.e. you're calling request, the code is continuing and running the second console.log(2-) call (which has the original values of activityXref) and then, at some point in the future, the request returns, the callback runs and outputs the first console.log(1-) call (which now contains the modified activityXref).

While there are ways to force "blocking" IO in node.js, that really defeats the purpose of node.js!

You mention using the values of activityXref "further down the line" so it sounds like you need to think about the architecture of your code in an event-driven context.

For example you could create your own function "doSomethingWithTheResults(()" and call that in the callback from request - when that function is called it will be after the activityXref has been updated, so it will see the new values.

1 Like

Thanks for the explanation, that makes sense. I don't have much experience with javascript if you can't tell. Now I need to figure out what to do with this info. :sunglasses:

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.