0

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.

5
  • 5
    Use var e = [{}];. Otherwise it isn't an array (so you can't access the 0th item) and doesn't have any objects in it (so you can't set any hello property) Commented Aug 13, 2013 at 20:59
  • 3
    e is undefined not an array of objects (you never assigned a value to e). Why do you think JS should think it is an array of objects? Commented Aug 13, 2013 at 21:00
  • Well, auto-vivification isn't in JS feature list, but it might have been. )) What I'm really wondering about is why this approach (working with array of objects) is even needed here. Commented Aug 13, 2013 at 21:06
  • updated to show the real codes Commented Aug 13, 2013 at 21:18
  • But maybe data.elements doesn't exist. What does console.log(data) show you? Commented Aug 13, 2013 at 22:11

2 Answers 2

3

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!"}];
Sign up to request clarification or add additional context in comments.

3 Comments

or e[0]["hello"] = "hello"
The problem is that my var is part of the "data" var comes of the server, so I have a array of objects as a part of "data".
@user2657856: You are not even doing anything with data in your example code. If you simplify your code so much that it changes the problem, don't wonder that you get (maybe) unhelpful answers.
0

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?

1 Comment

I tested declaring: var a= data.elements[0], isnt empty, it has some value, also data.elements[0].tocado is set.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.