Sub topic 7
Working with data and error from the wire
This chapter assumes you know JavaScript modules and async/await, the structure of a Lightning web component, the @api decorator, and how recordId is set on a record page. It also assumes you have already wired getRecord to a property. Here you will learn what the wire hands back and how to handle each state.
The shape of a wire result
A wire adapter provisions an object with two properties, data and error. Only one of them holds a value at a time. The other is undefined.
| State | `data` | `error` |
|---|---|---|
| Before the first response | `undefined` | `undefined` |
| Success | The record | `undefined` |
| Failure | `undefined` | An error object |
The first row matters. Both properties are empty until the wire responds, so an empty data does not mean the record failed to load.
Wiring to a property
When you wire to a property, the wire service replaces the property value each time a new result arrives. The template reads account.data and account.error directly.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue, getFieldDisplayValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import INDUSTRY_FIELD from '@salesforce/schema/Account.Industry';
export default class AccountSummary extends LightningElement {
@api recordId;
@wire(getRecord, {
recordId: '$recordId',
fields: [NAME_FIELD],
optionalFields: [INDUSTRY_FIELD]
})
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
get industry() {
return getFieldDisplayValue(this.account.data, INDUSTRY_FIELD);
}
}The template checks each property before it renders anything.
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
<p>Industry: {industry}</p>
</template>
<template lwc:if={account.error}>
<p>The record could not be loaded.</p>
</template>
</template>Nothing renders while the wire is waiting. That is often acceptable, but you can add a third branch for a loading message when neither data nor error is set.
Wiring to a function
A property is the simplest option. If you need to run logic for each result, wire to a function instead. The wire service calls the function with an object that has data and error, and you destructure it in the parameter list.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountNameLogger extends LightningElement {
@api recordId;
name;
errorMessage;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
wiredAccount({ data, error }) {
if (data) {
this.name = getFieldValue(data, NAME_FIELD);
this.errorMessage = undefined;
} else if (error) {
this.errorMessage = error.body && error.body.message;
this.name = undefined;
}
}
}Each branch clears the value from the other branch. Without that, a stale name could stay on screen next to a new error message, or the reverse.
Reading the error
The error object from a failed read is a FetchResponse-style object. The readable text is usually at error.body.message. Some failures return a different body shape, so guard the access as the examples do with error.body && error.body.message. Otherwise, a network problem could throw a second error inside your error handler.
Common mistakes
- Reading
this.account.data.fields.Name.valuedirectly. Ifdatais stillundefined, this throws. UsegetFieldValue, which you will meet in the next chapter, or checkdatafirst. - Treating a missing
dataas a failure. Checkerrorto detect failure. - Mutating
data. The object is read-only and shared through the cache, so copy values out of it instead of changing it. - Forgetting to clear old state when wiring to a function.
Exercise
A component wired to a property shows a blank panel, with no data and no error message. What does that tell you about the wire, and what could you add to the template?
Show answer
Both data and error are undefined, so the wire has not responded yet. Add a branch that renders a loading message when neither account.data nor account.error is set, for example with a getter that returns true in that case.
Exercise
In a function-wired component, why does the success branch set this.errorMessage = undefined?
Show answer
The wire can deliver a new result after an earlier failure. Clearing the error stops an old message from appearing next to fresh data.