You need to sign in to do that
Don't have an account?
Michael 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:
Cannot read property 'fields' of undefined
What is wrong and why can't I access the fields/value?
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?
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
Or
https://rajvakati.com/2019/02/01/lightning-web-components-wire-service-3/
https://rajvakati.com/2019/01/28/getlistui-wire-adapter/
All Answers
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
Or
https://rajvakati.com/2019/02/01/lightning-web-components-wire-service-3/
https://rajvakati.com/2019/01/28/getlistui-wire-adapter/
My code looks now like this and it's working as inteded:
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.
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>