0

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);
    });
};
1
  • So you have defined an init() method with 1 argument and don't pass any when you invoke it. Commented Feb 13, 2014 at 7:42

1 Answer 1

1

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

Comments

Your Answer

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