Skip to main content
1 of 2
satishW
  • 13
  • 1
  • 5

Array keep empty after push object from forEach

I am calling one helper method from another helper method using Promise to wait for the result. Need Help, I have problem "JsonArray" Array Keep Empty After pushing object from forEach, do I miss something ? here's the code :

        var action = component.get("c.getRecord");
        action.setParams({ 
            param : 'xyz',

        });
        action.setCallback(this, function(response) {
            var state = response.getState(); 
 
            if (state === "SUCCESS") {
            
                let responseJSON;
                try {
                    responseJSON = JSON.parse(response.getReturnValue());
                  
                }
                catch (err) {
                    
                }

                let name;
                let JsonArray=[];
                const that = this;
               
                responseJSON.forEach(key=> {
                    that.getClauses(component, event, helper,key.Name)
                    .then(function(result) {
                        JsonArray.push(result);

                    });
                });
                console.log('JsonArray '+JSON.stringify(JsonArray)); //got empty here

            }
            else if (state === "ERROR") {
                var errors = response.getError();
                if (errors && errors[0] && errors[0].message) {
                    
                }
            }

        });
        $A.enqueueAction(action);
    } ```


```getClauses : function(component, event, helper, name) {
        
       return new Promise(
        $A.getCallback(function(resolve, reject) {
        var action = component.get("c.getClausesByName");
        action.setParams({ 
            name :name.trim()
        });
        action.setCallback(this, function(response) {
            var state = response.getState();
            
            //console.log('jsonResp '+JSON.stringify(response.getReturnValue()));
            
            if (state === "SUCCESS") {
                
                
                var jsonResp = JSON.parse(response.getReturnValue());
                
                let JSONData = new Object ();
                JSONData.Code=jsonResp.Code;
                JSONData.Name= jsonResp.Name;
                
                resolve(JSONData);
            }
            else if (state === "INCOMPLETE") {
            }
                else if (state === "ERROR") {
                    var errors = response.getError();
                    if (errors) {
                        if (errors[0] && errors[0].message) {
                            console.log("Error message: " + 
                                        errors[0].message);
                        }
                    } else {
                        console.log("Unknown error");
                    }
                }
        });
        $A.enqueueAction(action);
            })
        );
    }```
satishW
  • 13
  • 1
  • 5