Skip to main content
added 5 characters in body
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

I am calling one helper method from another helper method using Promise to wait for the result. Need Help, II have problem "JsonArray" Array Keep Empty Afterwhere JsonArray array is empty after pushing object from forEach, do. Did I miss something  ? here'sHere'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);
            })
        );
    }```
loadRecords : function(component, event, helper) {
        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);
            })
        );
    }

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);
            })
        );
    }```

I am calling one helper method from another helper method using Promise to wait for the result. I have problem where JsonArray array is empty after pushing object from forEach. Did I miss something? Here's the code:

loadRecords : function(component, event, helper) {
        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);
            })
        );
    }
Source Link
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);
            })
        );
    }```