2

I'm trying to display the account of the logged in community user. Right now i'm only trying to display the Id but it's returning [object Object]. Maybe i'm not going about it the right way. Here is the code:

JS

import { LightningElement, wire } from 'lwc';

import getAccountByCommunityUser from '@salesforce/apex/AccountController.getAccountByCommunityUser';
import userId from '@salesforce/user/Id';

export default class PracticeCard extends LightningElement {

    @wire(getAccountByCommunityUser, {Id: userId})
    accounts;

    get accId() {
        return this.accounts.data.fields.Id.value;
    }
}

HTML

<template>
<lightning-card title='Accounts' icon-name='standard:account'>
    <div class='slds-m-around_medium'>
        <template if:true={accounts.data}>
            <li key={accounts.accountId}>
                {accounts.accountId}
            </li>
        </template>
        <template if:true={accounts.error}>
            {accounts.error}
        </template>
    </div>
</lightning-card>
</template>

Apex method

public with sharing class AccountController {

   @AuraEnabled(cacheable = true)
   public static List<Account> getAccountByCommunityUser(String Id) {
       Id communityUserContactId = [SELECT Id, Name, ContactId FROM User WHERE Id = :Id].ContactId;
       Id userContact = [SELECT Id, AccountId FROM Contact WHERE Id = :communityUserContactId].AccountId;
      return [SELECT Id, Name FROM Account WHERE Id = :userContact];
   }
}

2 Answers 2

2

It looks like your issue is purely in the HTML markup, try this:

<template>
<lightning-card title='Accounts' icon-name='standard:account'>
    <div class='slds-m-around_medium'>
        <template if:true={accounts.data}>
            <ul>
                <li for:each={accounts.data} for:item="acc" key={acc.Id}>{acc.Name}</li>
            </ul>
        </template>
        <template if:true={accounts.error}>
            {accounts.error}
        </template>
    </div>
</lightning-card>
</template>

EDIT Corrected markup

3
  • Ahh I'm getting errors when I try to deploy that. LWC1071: Missing key for element <ul> inside of iterator. Appreciate any additional help. At least I know I can just investigate more into the markup
    – boogie man
    Jul 24, 2019 at 4:29
  • @boogieman try the updated code, apologies for that
    – zaitsman
    Jul 24, 2019 at 4:58
  • you rock! Thanks a ton
    – boogie man
    Jul 24, 2019 at 5:20
0
<template if:true={accounts.data}>
        <li key={accounts.accountId}>
            {accounts.accountId}
        </li>
    </template>

Here you are checking if data exists but there is no iteration. As you have put in javascript you data ()which is array of records in accounts and you cannot get accountId directly. Moreover you are returning accounts directly so it is Id you are looking for, nor accountId.

Try this: (as pointed by @zaitsman)

<template if:true={accounts.data}>
            <template for:each={accounts.data} for:item="account">
                <p key={account.Id}>{account.Name}</p>
            </template>
        </template>

further read: https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .