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 })
  • 2
    With the way you have the fields should be written as fields: fields and changing it to CONTACT_FIELDS should be as fields: CONTACT_FIELDS. Is your current code snippet correct? – Jayant Das Jun 25 '19 at 20:29
  • Oh, derp. You're totally right... adding the key fixes it with the new variable name. Yet SOMEHOW the code shown above (fields without key name) also works. Maybe THAT's the (revised) question. I had lazily copied from a repo and didn't notice that this should be an object key/value pair. Does JS auto-set key name if only one variable is provided for an object key/value entry? – Brent Bow Jun 25 '19 at 20:34
  • I am not really sure how that worked if you didn't specify a value to 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. – Jayant Das Jun 25 '19 at 20:41

You seem to be missing the attribute fields to be passed correctly to the wire adapter getRecord(). The fields attribute expects a field name array. If you used a variable named fields to declare the array, then your code should be written as:

const fields = ['Contact.Name', 'Contact.Owner.Name', 'Contact.Owner.FirstName'];
@wire(getRecord, { recordId: '$recordId', fields: fields })

If you rename the fields variable to CONTACT_FIELDS then you should update the reference in the adapter too:

const CONTACT_FIELDS = ['Contact.Name', 'Contact.Owner.Name', 'Contact.Owner.FirstName'];
@wire(getRecord, { recordId: '$recordId', fields: CONTACT_FIELDS })
  • 1
    that is slightly inaccurate - the case where the variable is named fields is perfectly fine. That is a es6 shorthand - if the propertyname and fieldname are equal you can omit the value association and just write {field}. Just in case if the names are different (fields != CONTACT_FIELDS) you have to explicitly declare it (es6-features.org/#PropertyShorthand) – Renji-xD Jun 26 '19 at 11:14
  • Exactly this is wrong ! It should work with shorthand notation as well .You do not need key:value if value=key . – Mohith Shrivastava Jun 27 '19 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.