0

I have a LWC component to show navigation links to different list views. In .html it is written as below.

    <lightning-layout-item size="12" class="slds-p-vertical_xx-small">
                <lightning-layout multiple-rows="true" class="align-items-center">
                    <lightning-layout-item size="1">
                        <lightning-icon icon-name={url4Icon} size="small"></lightning-icon>
                    </lightning-layout-item>
                    <lightning-layout-item size="11" class="slds-p-left_small">
                        <div onclick={openFourthUrl} style="cursor: pointer;">{url4Label}</div>
                    </lightning-layout-item>

Need to show different layout items based on logged in user's role so as to show different navigation links to different user. New to LWC. Wondering how to achieve this I tried below in the JS to get the user details and keep a flag to populate it to true based on the role. getting error:Uncaught (in promise) TypeError: LWC component's @wire target property or method threw an error during value provisioning. Original error: [Cannot read property 'fields' of undefined]

     @track CreatorRoleVal;
areDetailsVisible=false;

// using wire service getting current user data
@wire(getRecord, { recordId: USER_ID, fields: ['User.Name', 'User.UserRole.Name'] })
userData({error, data}) {
    if(data) {
        window.console.log('data ====> '+JSON.stringify(data));

        let objCurrentData = data.fields;
        if(objCurrentData.UserRole.value != null)
        {
        this.CreatorRoleVal=objCurrentData.value.fields.Name.value;
        window.console.log('data ====> '+CreatorRoleVal);
        if(this.CreatorRoleVal==='Sales Lead')
        {
            this.areDetailsVisible=true;
        }
        }
        
    } 
    else if(error) {
        window.console.log('error ====> '+JSON.stringify(error))
    } 
}
2
  • 1
    What have you tried? Where are you stuck? The docs describe how to render elements conditionally. – David Reed Apr 7 at 23:22
  • @DavidReed i have updated more info. Can you have a look? – Swap31 Apr 8 at 15:14
0

Your JavaScript is accessing fields incorrectly:

        let objCurrentData = data.fields;
        if(objCurrentData.UserRole.value != null)
        {
        this.CreatorRoleVal=objCurrentData.value.fields.Name.value;

The layout of the returned Record object from the UI API is described here. It looks like you probably meant to assign

    this.CreatorRoleVal=objCurrentData.UserRole.value.fields.Name.value;
    

to get a nested object value.

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