0

Am quite new to Lightning and trying to understand Events so far i have created two components making Parent Child component with help of Events. But getting the error

"Action failed: c:MySampleChildCmp$controller$handleClick [Cannot read property 'setParams' of null] Failing descriptor: {c:MySampleChildCmp$controller$handleClick}"

Unable to identify the Error.

Here is the code.

ChildToParentEvent.evt:-

<aura:event type="COMPONENT">
    <aura:attribute type="String" name="firstName"/>
    <aura:attribute type="String" name="lastName"/>
    <aura:attribute type="String" name="Phone"/>
</aura:event>

MySampleChildCmp:-

<aura:component>
    <!--<aura:attribute type="String" name="userData" default=""/>
    <aura:attribute type="String" name="firstName" default=""/>
    <aura:attribute type="String" name="lastname" default=""/>
    <aura:attribute name="Phone" type="Integer" default=""/>-->
    <aura:registerEvent name="ChildToParentEvnt" type="c:ChildToParent"/>
    <lightning:button variant="brand" label="Brand action" title="Brand action" onclick="{! c.handleClick }" />
</aura:component>

MySampleChildCmpController:-

({
    handleClick : function(component, event, helper) {
        alert('called Child component');        
        var evt = component.getEvent("c.ChildToParentEvnt");
        evt.setParams({'firstName' : SFDC,
                        'lastName' : Developer,
                        'Phone' : 9999999
                       });
            evt.fire();
        alert('called Event');
    }
})

MySampleParentCmp:-

<aura:component >
    <aura:attribute type="String" name="userData" default="" />
    <aura:attribute type="String" name="firstName" default="ABC" />
    <aura:attribute type="String" name="lastName" default="DEF" />
    <aura:attribute type="Integer" name="Phone" default="" />
    <aura:attribute type="boolean" name="callChild" default="false" />
    <aura:handler name="ChildToParentEvnt" event="c:ChildToParent" action="{!c.handleClick}"/>
        <div>
           <c:MySampleChildCmp />
    </div>
    <div>
        <h2>User Data from Event </h2>
        Name    : {!firstName} {!lastName}<br/><br/>
        Phone    : {!Phone}
    </div>
</aura:component>

MySampleParentController:-

({
    handleClick : function(component, event, helper) {
        alert('Called Parent');
        var lastName= event.getParam('lastName');
        var firstName= event.getParam('firstName');
        var Phone= event.getParam('Phone');
        component.set('v.lastName', lastName);
        component.set('v.firstName', firstName);
        component.set('v.phone', Phone);
    }
})
  • Kindly format the code while asking question. – salesforce-sas Sep 23 at 16:20
4

Refer to this example. You are facing issue because of line component.getEvent("c.ChildToParentEvnt").

For getting view its v. and for getting controller method or apex method its c. BUT for getting event, you DO NOT have to use binder. Change the line to:

component.getEvent("ChildToParentEvnt")
  • Thanks peter, Now its working as expected – Ragavendra Sep 23 at 17:30

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.