-1

I have a two funtions in lightining controller such as doInit,navigate

 ({
    doInit: function(component, event, helper) {


var action = component.get("c.getdealid");
action.setParams({
            "primarytermsid": component.get("v.recordId")
        });
console.log("RecordID####" +v.recordId);
action.setCallback(this, function(response) {
    var state = response.getState();
    if (component.isValid() && state === "SUCCESS") {

       component.set("v.idx",response.getReturnValue());
    }
    else {
        console.log("Failed with state: " +state);

    }

});

// Send action off to be executed
$A.enqueueAction(action);
},

navigate:function(component,event,helper){




var idx = component.get("v.idx");

var navEvt = $A.get("e.force:navigateToSObject");
navEvt.setParams({
"recordId": idx,
"slideDevName": "detail"
});

  navEvt.fire(); 
}   

 })

Component:

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" controller="acquisitiondeallightningcntrl">
<aura:attribute name="deal" type="Acquisition_Deal__c[]"/>
    <aura:attribute name="primarytermsid" type="Primary_Terms__c"/>

   <aura:registerEvent name="navEvt" type="force:navigateToSObject"/>
     <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="idx" type="String" />


            <aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
               <table class="slds-p-around_x-small slds-text-body_small slds-table slds-table--bordered slds-table--fixed-layout " >
                   <thead>
                       </thead> 
                    <tbody>

                        <tr class="slds-hint-parent" >
                        <td><lightning:button variant="brand" label="Acquisition Deal" onclick="{!c.navigate}"/>


                        </td>


                           </tr>            

                    </tbody>
                </table>
</aura:component>

I want to use this idx variable from Doinit function to Navigate Function,when i am using this variable in Navigate Function it is showing error:

Uncaught Action failed: c:Redirectingthepage$controller$navigate [idx is not defined],please help me how to resolve this error

3

You are calling two methods from "doInit"

At the same time you are setting "idx" value in "doInit",So you are getting error.

you have to handle this situation like this: After getting "method1", you have to call another method (navigate).

controller:

  ({
 doInit: function(component, event, helper) {
     helper.method1(component, event, helper)

 }});

Helper:

({
     method1: function(component, event, helper) {

         var action = component.get("c.getdealid");
         action.setParams({
             "primarytermsid": component.get("v.recordId")
         });
         console.log("RecordID####" + component.get("v.recordId"));
         action.setCallback(this, function(response) {
             var state = response.getState();
             if (component.isValid() && state === "SUCCESS") {

                 component.set("v.idx", response.getReturnValue());
                 helper.navigate(component, event, helper);
             } else {
                 console.log("Failed with state: " + state);
             }

         });
         $A.enqueueAction(action);
     }
 },
 navigate: function(component, event, helper) {
     var idx = component.get("v.idx");

     var navEvt = $A.get("e.force:navigateToSObject");
     navEvt.setParams({
         "recordId": idx,
         "slideDevName": "detail"
     });

     navEvt.fire();
 } } })

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