1

I have below script in .js file.

@wire(getRecord, { recordId: '$recordId', fields: FIELDS })
wiredSandAnalysis({ error, data }) {
    if (data) {
        this.yValues = [];
        data.fields.forEach(field => { // here I am getting the error.
            this.yValues.push(field.value);
        });
    } else if (error) {
        this.error = error;
    }

I can see in debug logs that records are successfully returned by Apex method. But I am getting below error.

aura_prod.map.js:42142 TypeError: b.fields.forEach is not a function
    at wiredAccount (chartWrapper.js:4)
    at t.dispatchEvent (aura_prod.js:13)
    at Object.next (lds.js:2)
    at fa.next (lds.js:2)
    at Ca._nextWithErrorHandling (lds.js:2)
    at eval (lds.js:2)
    at Set.forEach (<anonymous>)
    at Ca.emitValue (lds.js:2)
    at eval (lds.js:2)
    at Ta.eval [as next] (lds.js:2)

Upon logging the result in the console I found that the fields are placed into originalTarget. but it should be in data.fields.

originalTarget

Am I missing something?

1 Answer 1

4

data.fields returns Object and not array, so Object.keys(data.fields) will give array of field API names. If you are trying to collect all the values of the fields, you can use below:

if (data) {
    this.yValues = [];
    Object.keys(data.fields).forEach((field) => {
        // here I am getting the error.
        this.yValues.push(data.fields[field].value);
    });
    console.log('Account => ', JSON.stringify(this.yValues));
} else if (error) {
    this.error = error;
}

For your reference, below is how you get response in getRecord:

SAMPLE RESPONSE:

{
  "apiName": "Account",
  "childRelationships": {

  },
  "fields": {
    "Description": {
      "displayValue": null,
      "value": "University of Boston _ from contact flow"
    },
    "Name": {
      "displayValue": null,
      "value": "University of Boston n"
    }
  },
  "id": "00128000009j45sAAA",
  "lastModifiedById": "00528000001IIBvAAO",
  "lastModifiedDate": "2019-08-25T14:37:49.000Z",
  "recordTypeInfo": null,
  "systemModstamp": "2019-08-25T14:37:49.000Z"
}

You must log in to answer this question.

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