You need to sign in to do that
Don't have an account?
Sajiv 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
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
an @AuraEnabled method has to be static.
@AuraEnabled
public static String getCurrentUserProfile(){...}
Cheers!
({
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