user3304932
user3304932

Reputation:

JS TypeError: Cannot read property 'map' of undefined

I'm getting a "Cannot read property of undefined" error in the following code.

What happens is that the initFromServer() is called which goes to get the model from the server. But what ends up happening is that when init() is called, the parameter model is undefined. Using console.log to determine what is going on it seems that it never reaches the Proxy.getModel() function before it determines that model is undefined.

I'm not entirely sure what is going on. There are a lot of other functions and what not that I have not inserted. Hopefully it makes sense.

ClientModel.prototype.initFromServer = function(success) {
    this.getProxy().getModel(this.init(), success());
}

ClientModel.prototype.init = function(model) {  
    this.setMap(new catan.models.Map(model.map.radius));    
};

In the proxy file we have:

Proxy.prototype.getModel = function(update, success) {
    jQuery.get("/game/model", function(data) {
                    update(data);
                    success(data);
    });
};

Upvotes: 0

Views: 2261

Answers (1)

user13500
user13500

Reputation: 3856

When you invoke init() in initFromServer() you do not pass any arguments.

In the init() function you access model as first argument …

ClientModel.prototype.initFromServer = function(success) {
    this.getProxy().getModel(this.init(), success());
                                       |
                                       +---- No Argument.
}

ClientModel.prototype.init = function(model) {
                                       |
                                       v
    this.setMap(new catan.models.Map(model.map.radius));
                                       |
                                       +----> Ups.
};

Upvotes: 1

Related Questions