I was surprised to find that changing the variable name for a JS const
from fields
to CONTACT_FIELDS
caused my component to stop functioning. (I realize this is a naming convention for Java constants, but I was using in my LWC.)
Specifically, changing fields
to CONTACT_FIELDS
in the below code (no other references to this variable in the .js file) will no longer retrieve the wired Contact fields.
Anyone know why??
const fields = ['Contact.Name', 'Contact.Owner.Name', 'Contact.Owner.FirstName'];
export default class Transfer extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields })
ownedRecord;
fields
should be written asfields: fields
and changing it toCONTACT_FIELDS
should be asfields: CONTACT_FIELDS
. Is your current code snippet correct?fields
, haven't come across it. Will try and then can explain better. But changing the variable name and being able to utilize that in the wire adapter, you'll need to pass it to the attribute.@wire(getRecord, { recordId: '$recordId', fields })
is because ES2015 allows shorthand notation for object literals, where instead of writing{ a: a, b: b, c: c }
, you can simply write{ a, b, c }
(this assumes a, b, and c are defined variables - this is why chaning the name offield
breaks it). So in this case,@wire(getRecord, { recordId: '$recordId', fields })
is the same as@wire(getRecord, { recordId: '$recordId', fields: fields })
.