function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sajiv Kartha 3Sajiv Kartha 3 

Creating Lightning component, accessing methods from controller js throws errors on browser.

Hi, I need help. I seem to have decently followed the steps of creating a lighting component. I have a page that has links and these links have to be shown specific to user profile.  I am trying all these through a helloWorld component... I am using Chrome.

I have created an apex class, that has just one method : Here it goes.

(1) Apex Class
------------------
public with sharing class helloWorldApex {

       @AuraEnabled
 public String getCurrentUserProfile()
    {
        System.debug('Invoking helloWorldApex class-------------------');
        System.debug('User Id is --------'+UserInfo.getUserId());
        System.debug('User Name is --------'+UserInfo.getFirstName()) ;
        User currentUser = [SELECT Profile.Name FROM User WHERE Id = :UserInfo.getUserId() ];
        String userProfile=currentUser.Profile.Name;
        
        if( currentUser.Profile.Name == 'System Administrator' ){
            System.debug('User does have the System Administrator Access----------');
            
        }
     return userProfile;   
    }
}
Executing ths class is fine in the Debug Console and the User Id and name do get printed in the logs.

Now, I am trying to access this method getCurrentUserProfile() from the component.
Here is my component code. It is named as 

(2) helloWorld.cmp
-----------------------
<aura:component implements="force:appHostable" controller="helloWorldApex">
    <h1>Quick Create Links</h1>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>

     <ui:outputText value="Your Profile is: "/>
     <ui:outputText value="{!v.currentUserProfile}"/>

< !-- Further here I see if the user profile has got specific profile and should show the links accordingly--> 
</aura:component>

(3) Here is the controller - helloWorldController.js

({
  doInit : function(component, event, helper) {
        debugger; //This does NOT get fired. Not sure why
        alert('doInit function invoked');  //this is getting fired in the browser.. 
    
       var action = component.get("c.getCurrentUserProfile"); //When control comes here, it throws an error in the browser ("Something has gone wrong. Cannot read property '$getActionDef$' of undefined. Please try again")

       alert('2');//this is not getting invoked
       action.setCallback(this, function(response) {
        var state=response.getState();
      alert();
      component.set("v.currentUserProfile", a.getReturnValue());
      });
    $A.enqueueAction(action);
   },
  
})

Can anyone tell me what I am missing ?

Thanks
Sajiv
 

 
Marco SchmitMarco Schmit
Hi,

an @AuraEnabled method has to be static.

@AuraEnabled
 public static String getCurrentUserProfile(){...}

Cheers!
Sajiv Kartha 3Sajiv Kartha 3
hmm.. I did that.. but still get the same error...  Is there anything else I need to do ?
Sajiv Kartha 3Sajiv Kartha 3
I got the answer...  It is in the js class... it has to be response.getReturnValue() and not a.getReturnValue() as I had earlier put.

({

     doInit : function(component, event, helper) {
      var action = component.get("c.getCurrentUserProfile");
      action.setCallback(this, function(response) {
      var state=response.getState();
      component.set("v.currentUserProfile", response.getReturnValue());
  });
    $A.enqueueAction(action);
   },

Now, I will proceed on this and hopefully my work should get completed