0

I have the following code and im trying to delete all empty child nodes. but the call to "removeEmptyChildNodes" is throwing the following error

Uncaught Error in $A.getCallback() [removeEmptyChildNodes is not defined] Callback failed: apex://GanttData/ACTION$getData Callback failed: apex://GanttData/ACTION$getData

can someone please help? below is my helper code

({
    getData : function (cmp) {
        var action = cmp.get("c.getData");
        action.setParams({
            recordid: cmp.get('v.recordId')
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var data = response.getReturnValue();
                var temojson = JSON.parse(JSON.stringify(data).split('items').join('_children'));
                console.log(temojson);
                removeEmptyChildNodes(JSON.parse(temojson));
                console.log(temojson);
                cmp.set('v.gridData', JSON.parse(temojson));
                cmp.set('v.loading', false);
            }
            // error handling when state is "INCOMPLETE" or "ERROR"
        });
        $A.enqueueAction(action);
    },
    removeEmptyChildNodes : function (app){
      app.forEach(function(d){
        if(d._children.length == 0){
          delete d._children;
          //console.log('_children:', d);
        }else if(d._children.length > 0){
            removeEmptyChildNodes(d._children)
        }
      });
    }
})
4
  • Have you tried this.removeEmptyChildNodes(JSON.parse(temojson));?
    – arut
    Jan 20, 2021 at 5:11
  • The error claims that removeEmptyChildNodes is not defined; therefore try moving its definition above the getData definition Jan 20, 2021 at 5:11
  • tried both the above, same error.
    – roy
    Jan 20, 2021 at 6:12
  • Using browser development tools call the function manually from the console to check if it is defined or not.
    – NBR
    Jan 20, 2021 at 9:06

2 Answers 2

1

If the above code shown is inside controller then write this.

const a = component.get('c.removeEmptyChildNodes');
$A.enqueueAction(a);

Or else if the above code is from helper then do this, but make sure helper is also passed as parameter in getData(cmp, event, helper) from controller:

helper.removeEmptyChildNodes(JSON.parse(temojson));

this might not work because setCallback has a function, so using this might limit the scope to that function.

2
  • my function "removeEmptyChildNodes " is in the helper. using your code did not work, same error.
    – roy
    Jan 20, 2021 at 7:25
  • Please edit your question and update your code above, and it should work, if its not working then you might have done something wrong. Jan 20, 2021 at 7:41
0

I think problem is when you call removeEmptyChildNodes the argument your are passing is getting undefined for some reason. You can try something like this to execute your recursive function.

 return this.removeEmptyChildNodes(d._children);
1
  • FYI. code becomes ~100 times slower if you are using delete in loops.
    – NBR
    Jan 20, 2021 at 9:01

You must log in to answer this question.

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