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);
    }
})
1
  • Kindly format the code while asking question. Commented Sep 23, 2019 at 16:20

1 Answer 1

5

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")
1
  • Thanks peter, Now its working as expected
    – Ragavendra
    Commented Sep 23, 2019 at 17:30

You must log in to answer this question.

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