I was having an issue with Node.js, socket etc. This may be an overall javascript question but i am having problems.
I don't know how to pull data out of a function and set it to a global var.
It's stuck in the below function and i cant effect the DOM (or html document, to keep the language simple). I am new to Node and socket.io and been working with Javascript for about a month now and slowly getting my head around but this seems like a simple question i cant get an answer to. Pull var from function and make global, should be simple?
var markets; // if I remove this I get Uncaught ReferenceError: markets is not defined and everything else breaks
console.log(markets); //this is undefined in the console, this is where the problem arises, how do i get the data from out of the below function?
var socket = io.connect('http://localhost');
socket.on('ready', function (data) {
console.log(data); // correctly outputs all data from server
for (var i = 0; i < data.length; i++) {;
item = data[i]; // just pulling one array from the database... i think?
createBox(item); //not really sure what this does
};
console.log(item); // this outputs one Object
markets = item; //this is defining item as being markets
});
function createBox(item) {
var box = document.createElement('div');
var itemName = document.createTextNode(item.name);
document.body.appendChild(itemName);
console.log(item.name);
};