socket.on("upvote", function(data)
{
document.getElementById("main").innerHTML = "";
foodCount = data.slice(0);
for (var i = 0; i < foodCount.length; i++)
{
var div = document.createElement("div");
div.style.width = "300px";
div.style.height = "300px";
div.style.backgroundColor = "red";
div.style.margin = "10px";
div.innerHTML = foodCount[i].name + " " + foodCount[i].location + " " + foodCount[i].deal + " " + foodCount[i].votes;
console.log(foodCount[i]);
div.addEventListener("click", function()
{
socket.emit("upvote", foodCount[i]);
});
document.getElementById("main").appendChild(div);
}
});
So basically, I have this bit of code on the front end js file that creates a div and adds an event listener of a click and uses socket.io to emit an upvote event with foodCount[i]. foodCount is an array I have of objects and the div events listeners are actually running through a for loop for each object in the foodCount array. The problem is, when I try to access a property of the foodCount[i] object on the backend socket server using say foodCount[i].name, it is coming up as null. I don't know if it's because I can't add event listeners like this or what, but the data is getting lost somehow, any insight?
socket.on("upvote", function(data)
{
console.log(data);
for (var i = 0; i < foodCount.length; i++)
{
if (foodCount[i].name == data.name)
{
foodCount[i].votes++;
}
}
io.sockets.emit("upvote", foodCount);
});
This is the bit of code on the backend socket server. As you can see, the data parameter is the specific object I passed from the front end server (foodCount[i]) and I am getting null when I try to do data.name in the for loop.