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
Tim Jones 71Tim Jones 71 

Cannot get my first LWC to work, any help greatly appreciated

I am sure its something simple but I am stuck. Been trying to get this to work for a bit.
Business case
Use the User Id to find a single record of a custom object they own.
In this example the Loyalty group is owned by the community user.

Appex Class
public with sharing class groupCordinator {
    @AuraEnabled(cacheable = true)
    public static List<Loyalty_Group__c> getLoyaltyGroup(String recordId) {
        return [SELECT Name FROM Loyalty_Group__c WHERE OwnerId = :recordId LIMIT 1];
    }
}
Java Script
import { LightningElement, wire, api, track } from 'lwc';
import uId from '@salesforce/user/Id';
import getRecord from '@salesforce/apex/groupCordinator.getLoyaltyGroup';

export default class CurrentUser extends LightningElement {
  userId = uId;
  @api recordId
  @track loyaltyGroup;

  @wire(getRecord, {recordId: '$recordId'})
  loyaltyGroup({ error, data }) {
      if (data) {
          this.loyaltyGroup = data[0];
      } else if (error) {
          console.log('Something went wrong:', error);
      }
  }

  get groupName() {
      return this.loyaltyGroup?.Name;
  }

}
HTML
<template>
    <lightning-card title="Current U">
        <p>Current User Id : {userId}</p>
        <p>Group Name : {groupName}</p>
    </lightning-card>
</template>

In the community the Current User ID populates but the name does not.
And the console does not show an error


 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Tim,

try to modify the js file  as like below.

JS
import { LightningElement, wire, api, track } from 'lwc';
import uId from '@salesforce/user/Id';
import getRecord from '@salesforce/apex/groupCordinator.getLoyaltyGroup';

export default class CurrentUser extends LightningElement {
  userId = uId;
  @api recordId
  @track loyaltyGroup;

  @wire(getRecord, {recordId: '$recordId'})
  loyaltyGroup({ error, data }) {
      if (data) {
          this.loyaltyGroup = data[0];
      } else if (error) {
          console.log('Something went wrong:', error);
      }
  }

  get groupName() {
      return this.loyaltyGroup.Name;
  }

}

Let me know if still facing issues.

If this helps, Please mark it as best answer.

Thanks!!