0

I am trying to access fields of account from the property "accountData" inside HTML file. by using the "." operator ie, accountData.Phone however I am getting error "Cannot read property 'Phone' of undefined"

//component1.html
<template> 
    <template if:true={accountData}>
        <h2>Mobile number is {accountData.Phone}</h2> 
    </template>
</template>

//component1.javascript
import { LightningElement, wire,track,api } from 'lwc';
import getAccount from '@salesforce/apex/AccountController.getAccount';

export default class AccountRecord extends LightningElement {
    originalData;
    @api recordId;
    @track accountData;
    @wire(getAccount,{recordId:"$recordId"})
    accountRecordData({error,data}){
    if(data){
            console.log("Data",data.Name);
            this.accountData = data.AccountRecord;
            //this.originalData = data;
            
        }
        else if(error){
            console.log("ERROR",error);
            //this.accountData = undefined;
        }
    }

I am a newbie in salesforce any help is highly appreciated

2
  • what is the issue with ? this.accountData = data; this should work Jan 14, 2021 at 11:28
  • Don't know. That is why I posted this. Jan 15, 2021 at 0:30

1 Answer 1

0

It looks like there is an issue with your path and what data you are displaying. In general I would recommend console.log(data) that way you can visual what is returned. It can change based on what you are requesting and how.

I would recommend changing it to the following if you are just trying to grab fields from the record you are viewing. Example: Display Account Fields while viewing Account Record.

HTML:

<template> 
    <template if:true={accountData}>
        <h2>Mobile number is {accountPhone}</h2> 
    </template>
</template>

JS:

    @wire(getRecord, {recordId: '$recordId', fields: FIELDS})
    account;
       
    get accountPhone(){
        return getFieldValue(this.account.data, 'Account.Phone');
    }

Here is the documentation on the above:

https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.data_wire_example

You must log in to answer this question.

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