TransWikia.com

getParam Not Working on Simple Application Event

Salesforce Asked on December 21, 2020

I have a button which when pressed causes an application event to fire. Before it does this it sets a string value using setParam. The event is fired and handled successfully. However, when I try to console.log the string value from the event it just says undefined. Can anyone help?

Event:

<aura:event type="APPLICATION" description="MovieGenreEvent">
<aura:attribute name="movieYear" type="String"/>
</aura:event>

Component firing the event:

<aura:component description="MovieSearchSlider" implements="flexipage:availableForAllPageTypes" access="global">

<aura:attribute name="SliderValue" type="String" default="2003" />
<aura:registerEvent name="MovieGenreEvent" type="c:MovieGenreEvent" />
<lightning:button variant="brand" label="Fire!" iconName="utility:check" iconPosition="left" onclick="{!c.handleClick}" />

</aura:component>

Controller:

    handleClick : function(component, event) {

        console.log("PRESSED");
        let sliderValue = component.get("v.SliderValue");
        var applicationEvent = $A.get("e.c:MovieGenreEvent");
        applicationEvent.setParams({"movieYear" : sliderValue});
        applicationEvent.fire();
        console.log("FIRED");
    }

Component Receiving the event:

<aura:component description="movieSearchList" controller="movieSearchController" implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global">
<aura:attribute name="movieYearFilter" type="String"/>
<aura:handler event="c:MovieGenreEvent" action="{!c.handleEvent}" />
</aura:component>

Controller:

    handleEvent : function(component, event, helper) {

    console.log("handled!");
    console.log('event = ' + event.getParam("movieYear"));
}

Logs:

PRESSED
handled!
event = undefined
FIRED
{setParams: ƒ, setParam: ƒ, fire: ƒ, getName: ƒ, getParam: ƒ, …}

One Answer

This is very strange but I just added a boolean attribute to the event, components and controllers. Not only did this arrive in the handler but it also seems to have caused the string to come through too. I have since removed the boolean, and the string is still there. It's possible that I did not save one of the aura definitions however this really doesn't seem likely given the logging I added. Otherwise maybe it's a bug on my instance.. Either way thanks to @Kal and @Jayant for your assistance, much appreciated.

Answered by Jay on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP