1

I'm trying to call a function every 10 seconds. But I'm getting this error: Uncaught Error in $A.getCallback() [Cannot read property 'setCallback' of undefined]. What is wrong?

doGet : function(component, event, helper) {
    let that = this;
    let getTools = component.get("c.getTools");

    getTools.setCallback(this, function(response) {
        let state = response.getState();
        if (state === "SUCCESS") {
            component.set("v.toolObjects", response.getReturnValue());
        }
    });           
    $A.enqueueAction(getTools);

    window.setTimeout(
        $A.getCallback(function() {
            console.log('Calling');
            that.doGet(component);
        }), 10000
    );
},

2 Answers 2

0

you are calling the wrong variable in your setCallback

let that = this;
let getToolsAction = component.get("c.getTools");

getToolsAction.setCallback(this, function(response) {...
$A.enqueueAction(getToolsAction);
1
  • Sorry. Yes. Changed. But the error is the same. Jul 12, 2019 at 7:28
0

Looks like you have written doGet method in controller.js instead of helper.js.

 window.setTimeout(
        $A.getCallback(function() {
            console.log('Calling');
            that.doGet(component);
        }), 10000
    );

In above code that.doGet(component); will always throw error as that will always undefined in controller.js.

Try Change Code like below should resolve your problem.

Controller.js

doGet : function(component, event, helper) {
        helper.doGet(component, event);
    }

helper.js

doGet:function(component,event) {
        let that = this;
        let getAccounts = component.get("c.getAccounts");

        getAccounts.setCallback(this, function(response) {
            let state = response.getState();
            if (state === "SUCCESS") {
                component.set("v.toolObjects", response.getReturnValue());
            }
        });           
        $A.enqueueAction(getAccounts);

        window.setTimeout(
            $A.getCallback(function() {
                console.log('Calling');
                that.doGet(component);
            }), 100
        );
    },
2
  • No. It's actually in helper :) I call it in controller in doInit. Jul 12, 2019 at 11:43
  • Actually you have added helper in function parameter so thought you have created in controller. Can you please add log after line let getTools = component.get("c.getTools"); to get getTools has value or not? Jul 12, 2019 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .