Sub topic 4
Wiring to a Property and Rendering Data and Error
This chapter assumes you can read basic JavaScript (ES modules, destructuring), you know the structure of a Lightning Web Component (HTML template, JavaScript class, @api properties), and you have already exposed a cacheable Apex method and imported it into a component. Here you connect that import to a property and show the result in the template.
What wiring to a property does
When you put @wire on a property, the platform assigns that property an object. The object has two fields:
data: the value the Apex method returned. It isundefineduntil the call succeeds.error: the failure details. It isundefinedunless the call fails.
You never assign to the property yourself. The wire service replaces its value each time it provisions new results.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}The first argument to @wire is the imported Apex method. The second is an object that maps Apex parameter names to values. The '$recordId' string makes the parameter reactive, which a later chapter covers. For now, read it as "use the current value of this.recordId".
The property contacts is the decorated field. In the template you reach the results as contacts.data and contacts.error.
Rendering data and error
Because data and error start out undefined, the template needs to handle three states: loading, success, and failure. The lwc:if, lwc:elseif, and lwc:else directives do this.
<template>
<template lwc:if={contacts.data}>
<ul>
<template for:each={contacts.data} for:item="contact">
<li key={contact.Id}>{contact.Name}</li>
</template>
</ul>
</template>
<template lwc:elseif={contacts.error}>
<p>Could not load contacts.</p>
</template>
</template>Three details matter here:
- Each item in a
for:eachloop needs akey. Use a stable unique value such as the recordId, not the loop index. contacts.datais truthy even when it is an empty array, so an account with no contacts renders an empty list, not the error branch.- While the request is in flight, neither branch is true, so the component renders nothing. Add an
lwc:elsebranch if you want a spinner.
The diagram shows how the wire service moves the property through these states.
Showing a useful error message
The error object from an Apex call is not a plain string. For an Apex exception, the readable message is in error.body.message. Other failures, such as a network problem, might not have a body, so a defensive getter checks for both shapes.
get errorMessage() {
const error = this.contacts.error;
if (!error) {
return undefined;
}
return error.body ? error.body.message : error.message;
}Add this getter to the class, then use {errorMessage} in the template in place of the fixed text.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Nothing renders and no error appears | The request is still pending, or a reactive parameter is `undefined` | Check that `recordId` has a value. A wire with an `undefined` reactive parameter does not call Apex. |
| `[object Object]` on the page | The whole error object is bound to the template | Use `error.body.message` |
| Error that the method is not cacheable | The Apex method lacks `cacheable=true` | Add `@AuraEnabled(cacheable=true)` to the method |
| Assigning to `this.contacts` has no effect | The wire service owns the property | Use a wire function when you need to store your own copy |
Exercise
An account has no contacts. The Apex method returns an empty list. Which template branch renders, the contacts.data branch or the contacts.error branch, and why?
Show answer
The contacts.data branch renders. An empty array is a defined object, which is truthy in a template condition, so the list renders with no items. The error branch applies only when the call fails and error is set.
Exercise
Write the getter that returns a readable message for contacts.error, and explain why it checks error.body.
Show answer
get errorMessage() { const error = this.contacts.error; if (!error) { return undefined; } return error.body ? error.body.message : error.message; }. Apex exceptions put the message in error.body.message, while other failures such as network errors have no body and expose error.message instead.