1

I need to understand where I am going wrong in the below piece of code. The error screenshot is shown below. enter image description here

HTML Template:-
<template>
   {name} 
</template>
 JS Controller:-
import { LightningElement,api,wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import LASTNAME_FIELD from '@salesforce/schema/Contact.LastName';

export default class FetchRecords extends LightningElement {
    @api recordId;
    @wire(getRecord, { recordId: '$recordId', fields:LASTNAME_FIELD})
    contact;
        get name(){
            return this.contact.data.fields.LastName.value;
        }
}
4
  • Try return this.contact.LastName; assuming recordId is a Contact record Id I would expect contact to be a Contact SObject. Though you may also need a null guard as the data is loaded asynchronously return this.contact ? this.contact.LastName : '';.
    – Keith C
    Jan 5, 2021 at 17:00
  • @KeithC...using this.contact.LastName resolved the error but now in HTML template nothing is coming. What could be the reason for that?
    – SFDC LWC
    Jan 5, 2021 at 17:15
  • That is because after the wired contact is populated, there's no reactive property which would make the component rerender. You can try to test this by forcing the rerender, just put {contact} within your template, and this should render it correctly, just to test. Jan 5, 2021 at 17:25
  • @PrashantKashyap..in this case its displaying [object OBJECT] on the template....i tried using{contact.data}...same result.
    – SFDC LWC
    Jan 5, 2021 at 17:32

1 Answer 1

1

I believe the issue might be get might be getting called before wire gets executed as it is directly called without using getFieldValue.

The correct syntax as per documentation is to use getFieldValue

import { getRecord, getFieldValue } from 'lightning/uiRecordApi';


@wire(getRecord, { recordId: '$recordId', fields:LASTNAME_FIELD})
    contact;
        get name(){
            return getFieldValue(this.contact.data, LASTNAME_FIELD);
        }
1
  • Awesome man @Nagendra for helping me out..it was the real thorn in the flesh.
    – SFDC LWC
    Jan 5, 2021 at 17:23

You must log in to answer this question.

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