0

I am not able to get the user id to get concatenated at the end of the URL for the profile.

Controller class:

@AuraEnabled
public static User getUser() {
    User myUser = [SELECT Id, Name 
            FROM User WHERE Id =: UserInfo.getUserId() LIMIT 1];

    if(myUser != NULL){
        System.debug('User: ' +myUser);
        return myUser;  
    } else{
        return NULL;
    }
 }

Component's JSController: I have implemented doInit method to initialize User object on the Component and have the user name in the Menu.

Cmp:

<li><ui:outputText value="{!v.user.Name}"/> 

                      <lightning:buttonMenu iconName="utility:down" name="Menu" iconSize="small" onselect="{!c.handleMenuSelect }">
                        <lightning:menuItem label="My Profile" value="Profile"/>
                        <lightning:menuItem label="Logout" value="Logout"/>
                      </lightning:buttonMenu>
                </li>

CmpJs

handleMenuSelect: function(cmp, event, helper) {

    var selectedMenuItemValue = event.getParam("value");
    if(selectedMenuItemValue == 'Profile'){
         var urlEvent = $A.get("e.force:navigateToURL");
         urlEvent.setParams({
             "url": '/profile/' + component.get("v.user.Id")
         });
         urlEvent.fire();  
    } else if(selectedMenuItemValue == 'Logout'){
       var urlEvent = $A.get("e.force:navigateToURL");
         urlEvent.setParams({
             "url": '/logout' 
         });
         urlEvent.fire(); 
    }
    console.log(selectedMenuItemValue);
}
})
  • 1
    By the way, your method should really just be written as: @AuraEnabled public static User getUser() { return [SELECT Name FROM User WHERE Id =: UserInfo.getUserId()]; } – sfdcfox May 12 '17 at 16:54
  • @sfdcfox Sure. I would remove the If-else statements. I think I might need Id field in the Select statement, as I need it to concatenate with profile URL. Correct me if I am wrong. Thanks – Arnold Jr. May 12 '17 at 17:35
  • You get Id "for free" whenever you do a query. There's no need to explicitly mention it unless it's the only field you're querying. – sfdcfox May 12 '17 at 17:37
  • @sfdcfox Sounds good. Could you please help me in solving my issue? :) – Arnold Jr. May 12 '17 at 17:40
1
         "url": '/profile/' + component.get("v.user.Id")

You should retrieve the attribute by its name and the go from there:

         "url": '/profile/' + component.get("v.user").Id

Also, make sure the function's parameter matches the variable name you're using:

handleMenuSelect: function(component, event, helper) {
  • Here is my doInit method JSController: ({ doInit : function(component, event, helper) { var action = component.get("c.getUser"); action.setCallback(this, function(response) { var state = response.getState(); if (component.isValid() && state === "SUCCESS") { component.set("v.user", response.getReturnValue()); } else { console.log("Failed with state: " + state); } }); $A.enqueueAction(action);}, – Arnold Jr. May 12 '17 at 17:55
  • Yayyy! It worked. Thank you so much. \m/ My bad, I didn't see the silly mistake I did with the function's parameter. – Arnold Jr. May 12 '17 at 18:13
1

Looks like you are passing in cmp to your handleMenuSelect function, but trying to reference it as component ...

try this:

urlEvent.setParams({
 "url": '/profile/' + cmp.get("v.user.Id")
})
  • Component seems to be working fine for logout URL. So I don't think I am using a incorrect reference. – Arnold Jr. May 12 '17 at 17:30
  • I apologize. I realized what you just said. Thank you for that. :) – Arnold Jr. May 12 '17 at 18:11

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.