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
Michael MuchowMichael Muchow 

LWC Wire Function "Cannot read property 'fields' of undefined"

Hi guys,
I'm trying to wire a function as described here (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.data_wire_service_about).

This is my code:
 
@wire(getRecord, { recordId: '$recordId', fields: ['Lead.Name'] })
    wiredRecord({ error, data}){
        if (data) {
            this.record = data;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.data = undefined;
        }
        console.log('test '+JSON.stringify(this.record));
    };

get test() { 
    return this.record.fields.Name.value; 
}
Everything works fine and I got the following log of the data:
{"apiName":"Lead","childRelationships":{},"fields":{"Name":{"displayValue":null,"value":"Test Name"}},"id":"00Q0D000001njilUAA","lastModifiedById":"005240000052XZeAAM","lastModifiedDate":"2019-03-21T13:41:40.000Z","recordTypeInfo":{"available":true,"defaultRecordTypeMapping":true,"master":false,"name":"Customer","recordTypeId":"01224000000SVMWAA4"},"systemModstamp":"2019-03-21T13:41:40.000Z"}
If I want to access the value like it described in the link (e.g. this.record.fields.Name.value;) with this.record.fields.Name.value; I get the error:

Cannot read property 'fields' of undefined

What is wrong and why can't I access the fields/value?

 
Best Answer chosen by Michael Muchow
Raj VakatiRaj Vakati
there are two ways you can do it one at the javascript controller end like below

It must be like this return this.record.data.fields.Name.value; 

https://developer.salesforce.com/docs/component-library/documentation/lwc/reference_wire_adapters_record
 
@track error ;
    @track email ; 
    @track name;
    @wire(getRecord, {
        recordId: USER_ID,
        fields: [NAME_FIELD, EMAIL_FIELD]
    }) wireuser({
        error,
        data
    }) {
        if (error) {
           this.error = error ; 
        } else if (data) {
            this.email = data.fields.Email.value;
            this.name = data.fields.Name.value;
        }
    }

Or
 
get name() {
        return this.contact.data.fields.Name.value;
    }
 
    get title() {
        return this.contact.data.fields.Title.value;
    }
 
    get phone() {
        return this.contact.data.fields.Phone.value;

    
    }
 
    get email() {
        return this.contact.data.fields.Email.value;
    }

https://rajvakati.com/2019/02/01/lightning-web-components-wire-service-3/

https://rajvakati.com/2019/01/28/getlistui-wire-adapter/

All Answers

Raj VakatiRaj Vakati
there are two ways you can do it one at the javascript controller end like below

It must be like this return this.record.data.fields.Name.value; 

https://developer.salesforce.com/docs/component-library/documentation/lwc/reference_wire_adapters_record
 
@track error ;
    @track email ; 
    @track name;
    @wire(getRecord, {
        recordId: USER_ID,
        fields: [NAME_FIELD, EMAIL_FIELD]
    }) wireuser({
        error,
        data
    }) {
        if (error) {
           this.error = error ; 
        } else if (data) {
            this.email = data.fields.Email.value;
            this.name = data.fields.Name.value;
        }
    }

Or
 
get name() {
        return this.contact.data.fields.Name.value;
    }
 
    get title() {
        return this.contact.data.fields.Title.value;
    }
 
    get phone() {
        return this.contact.data.fields.Phone.value;

    
    }
 
    get email() {
        return this.contact.data.fields.Email.value;
    }

https://rajvakati.com/2019/02/01/lightning-web-components-wire-service-3/

https://rajvakati.com/2019/01/28/getlistui-wire-adapter/
This was selected as the best answer
Raj VakatiRaj Vakati
And you can read it directly using dot notation also in the template tag 
Michael MuchowMichael Muchow
Thanks for your help.
My code looks now like this and it's working as inteded:
 
@wire(getRecord, { recordId: '$recordId', fields })
    wiredRecord({ error, data}){
        if (data) {
            this.record = data;
            this.error = undefined;
            this.checkRecordTypeName(this.record.fields.RecordType.displayValue);
            this.checkTargetCountry(this.record.fields.TargetCountry__c.value);
            this.checkCountry(this.record.fields.Country.value);
        } else if (error) {
            this.error = error;
            this.data = undefined;
        }
    };

Looks like the code example on this site (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.data_wire_service_about) at the bottom is not quiet right.
Paritosh BhatiaParitosh Bhatia
When using wire function, you need to check data is present in "record" before accessing its properties. For example

When using @wire on a function
<template if:true={record}>
        {test}
</template>

When using @wire on a property
<template if:true={record.data}>
        {test}
</template>
NK@BITNK@BIT