How can I access variable C?
test = function(){
a = 1;
b = 2;
c = a+b;
return c
}
console.log(c)
Returns:
Uncaught ReferenceError: c is not defined
EDIT: Please disregard above question
To elaborate more on this. I am new to JavaScript and am stuck on a block of code that I can't get to work. I tried to simplify the problem I was having but I think that may have backfired...
Here is the full code that I am having trouble with. I need to access the variables inside of getAPIData.onload
, But am getting "undefined"
What is the best way to access these values?
getData();
function getData(){
var activeMachines = [41, 44, 45]
var dict = {};
let data = activeMachines.reduce((obj, machineID) => {
var getAPIData = new XMLHttpRequest();
var url = 'http://127.0.0.1:8000/processes/apidata/' + machineID + '/';
getAPIData.open('GET', url);
getAPIData.send();
getAPIData.onload = function(){
var APIData = JSON.parse(getAPIData.responseText);
dict['temp' + machineID] = APIData[0].tempData;
dict['humid' + machineID] = APIData[0].humidData;
timeValue = String((APIData[0].dateTime));
dict['time' + machineID] = new Date(timeValue);
}
temp = dict['temp'+ machineID];
humidity = dict['humid'+ machineID];
time = dict['time'+ machineID];
obj['machine_'+ machineID] = {temp, humidity, time}
return obj
}, {})
console.log(data);
}
This returns the data dictionary, but all of the values are undefined. Any help is appreciated. I will read more into JavaScript scope as others have suggested.