0

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: ƒ, …}
  • 1
    Does console.log(sliderValue); in your handleClick method show the value prior to firing your application event? – Lightning Evangelist Oct 9 '18 at 19:49
  • Hi, thanks for the reply - I removed it but previously it said '2003' as this is the default value – Jay Oct 9 '18 at 19:54
  • I don't see anything obvious here as why it should not print the value. I infact used same code as yours and could find it working perfectly fine. Do you happen to have any other handler which is setting the values again and firing the event? – Jayant Das Oct 9 '18 at 20:11
  • Hi Jayant, there are no other handlers no. Thanks for taking the time run it though – Jay Oct 9 '18 at 20:23
0

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.

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.