You need to sign in to do that
Don't have an account?
Initialize controller constructor in Lightning
Hello,
I have a Lightning Component with an Apex controller. The controller has a constructor that sets some variables, but it doesn't seem to be running when I load my component because the variable is null when I call another function. I can't call it from the component either as I get a notification that "AuraEnabled does not apply to Constructors".
See below for an example of what I mean but my case is a lot more complex as I have quite a few variables that need to be set at the beginning and calling a SOQL query every time I call a function that needs these would not be efficient.
public class MyController{ public User currentUser; public MyController(){ currentUser = [Select Id, FirstName from User where Id =: UserInfo.getUserId();] } @AuraEnabled public static String getMyName(){ return currentUser.FirstName; } }
You just need to declare currentUser as static... if you find my solution helpful then please make this as best answer...
I noticed you changed the name of my constructor from the class name. Am I supposed to name it after a component?