Reputation: 131
I´m having some troubles with a array of objects parameter for a function.
REAL CODES:
function drawTable(x,y,numero,t,e) {
context.fillStyle = "#ffff66";
context.fillRect(x*146,y*146,55,55);
var color;
if (e[numero].tocado == "rojo") {
color = "#cc3300"
} else if (e[numero].tocado == "azul") {
color = "#0099ff";
} else {
color = "#66ff66";
}
context.fillStyle = color;
if (t==0 && x < 4) {
context.fillRect((x*146)+55,(y*146)+9,91,39);
}
if (t==1){
context.fillRect((x*146)+9,(y*146)+55,39,91);
}
if (x==4) {
if (y<4) {
if (t==1) {
drawTable(0,y+1,numero+1,0,e);
} else {
drawTable(0,y,numero,1,e);
}
}
} else {
drawTable(x+1,y,numero+1,t,e);
}
return;
}
socket.on("start", function (data) {
game = data;
drawTable(0,0,0,0,game.elements);
//....
}
"Data is a object:"
function Game() {
this.turno = "";
this.puntuacion_rojo = 0;
this.puntuacion_azul = 0;
this.elements = [];
}
"elements built"
this.elements.push({
id:
tipo:
tocado: ,
left:
top:
width:
height:
});
The error is:
Uncaught TypeError: Cannot read property 'tocado' of undefined.
in the line
if (e[numero].tocado == "rojo") {
Data isn't empty, I tested it, It returns from the server correctly.
Upvotes: 0
Views: 126
Reputation: 237080
It sounds like data.elements
is an empty array, so e[0]
returns undefined. Perhaps you meant to put that this.elements.push(…)
line inside the Game function?
Upvotes: 0
Reputation: 141877
So, the problem is that "cosa" "thinks" e isnt a array of objects
You're mistaken. cosa
isn't even ever called. The thread stops when an uncaught exception is thrown. Since e
is undefined, you get an exception on this line:
e[0].hello = "hello!";
If you want to access e[0]
, e
should be an array:
var e = [];
and if you want to access e[0].hello
, e[0]
should be an object:
var e = [];
e[0] = {};
e[0].hello = "hello!":
Or all of that in one line:
var e = [{hello: "hello!"}];
Upvotes: 3