0

I am trying to migrate code from Visualforce (working) to a Lightning Component. The update method should draw a graph and redraw it when the root node changes. I believe I am declaring the methods correctly but when I call "update" I get the above error. I've tried changing the function name in case it was a reserved keyword but get the same error. Any Suggestions?? Many thanks

Code looks like the following...

({
    doInit : function(component, event, helper) {

        var action = component.get("c.getNodeJSON");

        action.setCallback(this, function(response){       
            var data = JSON.parse(response.getReturnValue());
            component.set("v.root", data);
            update(component, root);
        });

        $A.enqueueAction(action);
    },

    update : function(component, source) {
        var root = component.get("v.root");
        // etc etc
    }
})
  • Were you able to call the update function? I tried using "this", it too didn't work, since it refers to "this" for action.setCallback – Malay Desai May 28 '16 at 17:41

2 Answers2

0

In Controller.js, you can't call another function of the controller from within the callback method. This is allowed in helper.js only. Either move the "update" method to helper.js and user helper.update() or move both doInit and update in the helper.js.

({
doInit : function(component, event, helper) {

    var action = component.get("c.getNodeJSON");

    action.setCallback(this, function(response){       
        var data = JSON.parse(response.getReturnValue());
        component.set("v.root", data);
        helper.update(component, root);
    });

    $A.enqueueAction(action);
},    

})

Then in your helper.js

({
    update : function(component, source) {
        var root = component.get("v.root");
        // etc etc
    }
})
Malay Desai
  • 518
  • 5
  • 6
-1

You can not call update directly. you need to add .this before calling update method. please try below updated code.

({ doInit : function(component, event, helper) {

    var action = component.get("c.getNodeJSON");
    var self = this;
    action.setCallback(this, function(response){       
        var data = JSON.parse(response.getReturnValue());
        component.set("v.root", data);
        this.update(component, root);
    });

    $A.enqueueAction(action);
},

update : function(component, source) {
    var root = component.get("v.root");
    // etc etc
}

})

Please make answer correct if this resolved your issue.