1

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;
4
  • 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, 2019 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? Jun 25, 2019 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, 2019 at 20:41
  • 2
    @JayantDas The reason it worked as @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 of field breaks it). So in this case, @wire(getRecord, { recordId: '$recordId', fields }) is the same as @wire(getRecord, { recordId: '$recordId', fields: fields }).
    – jbyrd
    Oct 11, 2021 at 18:14

1 Answer 1

3

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 })
2
  • 2
    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, 2019 at 11:14
  • Exactly this is wrong ! It should work with shorthand notation as well .You do not need key:value if value=key . Jun 27, 2019 at 3:17

You must log in to answer this question.

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