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.