0

can we pass parameters like json objects,json,strings, integers among helper methods in lightning components?

I am trying to pass

    (
     Helper1:function(){

    Var Parameter1s = jsonobject;
    Parameter1s.forEach(function(a, i){
    this.Helper2(a.fieldname[0]);
});


    },
    Helper2:function(parameter1){
    Alert('helper2');},
    );

Error: uncaught Action failed : "xxxx$controller$helper1[ cannot read property 'Helper2' of undefined]

2

its not clear how you are attempting to do so, but yes, you can:

For example:

({
    helper1 : function(cmp, evt) {
        var parameter1s = [{'a': 1}];
        parameter1s.forEach((a, i)=>{
            this.helper2(a);
        });
},
    helper2 : function(aVariable) {
        console.log(aVariable);
  },
})

after the couple of updates you have made to your code, there are still things wrong with it,

for example:

  • Var, should not be capitalized.
  • jsonobject is not iterable by forEach
  • and it goes without saying, you have to do this from within the helper, not the controller.
5
  • Sorry wasn't clear in my question, calling another helper method(helper2) in loop from helper 1 method. It is throwing the above error. I have checked the method name it is same infact copied the same name. – dennisthemenace Apr 15 '19 at 2:42
  • so, you did a typo? – glls Apr 15 '19 at 2:52
  • No I didn't do a typo, well I tried the call outside the for each loop . Calling this.Helper2 and it is going to Helper2 method. But when I am calling it in for each loop the error shows up – dennisthemenace Apr 15 '19 at 2:54
  • var with capital V wont work – glls Apr 15 '19 at 3:15
  • @dennisthemenace - fyi: javascript is case sensitive – glls Apr 15 '19 at 3:17
0

@dennisthemenace - If you are trying to call a helper method inside the for each loop with function type use helper.helperMethod2() and make sure the your helperMethod1 has helper argument passed to it. i.e helperMethod1(component, event, helper).

Ex: array.forEach(function callback(currentValue[, index[, array]]) { }[, thisValue]);

Another argument thisValue is used to tell the function to use "this" value when executing argument function. If this parameter is empty, the value "undefined" will be passed as its "this" value. Hence In this scenario we need to explicitly use "helper" instead of "this".

5
  • this would only be true if called from the controller, not the helper, asides, there are bunch of other errors in the op's code – glls Apr 15 '19 at 4:00
  • @glls yes, I agree there are some errors in the code posted. But looking at the description, he is facing issue while calling helper method inside for each loop. As analyzed 'this' keyword is not recognized when we try to call other helper method inside for each loop. We have to explicitly use 'helper' to call other helper methods inside for each loop even in the helper method. – Aqeeb Ahamed Apr 15 '19 at 10:28
  • again, this is only true if invoked from the controller – glls Apr 15 '19 at 11:25
  • @glls if "this" is used in normal scenarios it will work in helper as you mentioned. But inside foreach the scenario would be different. eg: arr.forEach(function callback(currentValue[, index[, array]]) { }[, thisArg]); Another argument thisValue is used to tell the function to use this value when executing argument function. If this parameter is empty, the value "undefined" will be passed as its "this" value. Hence In this scenario we need to explicitly use "helper" instead of "this". – Aqeeb Ahamed Apr 16 '19 at 6:58
  • I will update the answer so that it is clearly defined. – Aqeeb Ahamed Apr 16 '19 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.