Sub topic 11
Why a Refresh Does Nothing: Common Mistakes
This chapter assumes you can write a basic LWC with @wire, you know @AuraEnabled(cacheable=true) Apex methods, and you are comfortable with async/await. It also assumes you have seen refreshApex used once.
refreshApex fails quietly. A wrong call usually produces no error, no console warning, and no visible change. The component still shows old data, and you can't tell whether the save failed, the refresh failed, or the screen failed to update. This chapter lists the common causes and gives you a way to find which one you have.
What refreshApex needs
refreshApex takes one argument: the exact object a wire provisioned. For a wired Apex method, that object has a data property and an error property. The function uses that object to find the wire it belongs to. It then clears the cached response for that wire, calls the server again, and provisions the new result.
It also returns a Promise. That gives you two ways to go wrong: you can pass the wrong object, or you can treat the Promise carelessly. Most failures fall into one of those two groups. A few come from the server side instead.
Mistake 1: Passing a piece of the result
Passing data, or a copy of the result, is the most common mistake. The link to the wire is carried by the original object, so a fragment or copy has no link.
// Wrong: refreshApex needs the object the wire provisioned, not a part of it.
await refreshApex(this.contacts.data);
// Wrong: copying the values into your own object loses the link to the wire.
await refreshApex({ data: this.contacts.data, error: this.contacts.error });
// Right: pass the original wired result.
await refreshApex(this.contacts);The same error appears with spread syntax ({ ...this.contacts }) and with JSON.parse(JSON.stringify(...)). Both produce a new object that looks right when you log it.
Mistake 2: A function wire that keeps only the data
When a wire is bound to a function, the framework calls your function with the result object. If you destructure it and keep only data, you have nothing to pass later.
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListFunctionWire extends LightningElement {
@api recordId;
contacts;
error;
wiredContactsResult;
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
// Keep the whole result object, not only result.data.
this.wiredContactsResult = result;
const { data, error } = result;
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
async handleRefresh() {
await refreshApex(this.wiredContactsResult);
}
}The line this.wiredContactsResult = result; is the one people forget. Without it, this.contacts is only the array, so any call to refreshApex(this.contacts) falls under Mistake 1. Store the result each time the function runs. A refresh calls the function again with a new result object, and the old one is no longer the right argument.
Mistake 3: Not waiting for the save
refreshApex re-reads whatever is on the server at the moment it runs. If the update has not finished, the refresh can read the old values and report success.
// Wrong: both calls start at once, so the read may happen before the write.
updateEmail({ contactId, email });
refreshApex(this.contacts);
// Right: wait for the save, then refresh.
await updateEmail({ contactId, email });
await refreshApex(this.contacts);The second await also matters. Without it, any code after the refresh runs before the new data arrives, and errors from the refresh are lost.
Mistake 4: Refreshing an imperative call
refreshApex only works on results that a wire provisioned. If you call the Apex method directly, you get a plain array back, with no connection to any wire.
import getContacts from '@salesforce/apex/ContactController.getContacts';
import { refreshApex } from '@salesforce/apex';
async loadContacts() {
this.rows = await getContacts({ accountId: this.recordId });
}
async handleSave() {
// Wrong: this.rows came from an imperative call, so there is no wire to refresh.
await refreshApex(this.rows);
}For imperative calls, call loadContacts() again. Note that a cacheable method called imperatively can still return a cached response, so a plain repeat call might also show old data. If that happens, the usual choice is to move the read onto a wire and use refreshApex.
Mistake 5: No result exists yet
A wire with a reactive parameter such as '$recordId' does not run while that parameter is undefined. Until it runs, there is no result object. A refresh button clicked early, or a save that completes before the first load, passes undefined to refreshApex.
Check for this when the refresh is not tied to a user action that implies loaded data:
async handleSave(event) {
const { contactId, email } = event.detail;
await updateEmail({ contactId, email });
if (this.contacts) {
await refreshApex(this.contacts);
}
}Mistake 6: Replacing the wired property
With a property wire, the framework assigns the result object to your field. If your own code assigns something else to that field, the field no longer holds the provisioned object.
@wire(getContacts, { accountId: '$recordId' })
contacts;
handleReverse() {
// Wrong: this creates a new object that the wire did not provision.
this.contacts = { data: [...this.contacts.data].reverse() };
}After this assignment, refreshApex(this.contacts) gets your object. Keep wired fields read-only. Put derived values such as sorted lists in a getter or in a separate field.
get sortedContacts() {
return this.contacts.data
? [...this.contacts.data].sort((a, b) => a.Name.localeCompare(b.Name))
: [];
}Wire results are also read-only, so copy the array before you sort or reverse it, as the example does.
Mistake 7: The refresh works but the server returns the same data
Sometimes the refresh runs correctly and the screen is still the same, because the server response has not changed. Check these server-side causes:
- The SOQL query does not select the changed field, or the
WHEREclause filters out the changed record. - The update ran in a different transaction that has not committed, for example a queued or asynchronous process.
- Sharing or field-level security hides the record or field from the current user. This applies when the query uses
WITH USER_MODE. - The
LIMITcuts off the record that changed.
To tell this case from the others, open the browser's Network tab and look for the second Apex request after the save. If the request exists and the response has the old values, the component is correct and the Apex or the data is the problem. If no request exists, go back to Mistakes 1 to 6.
Mistake 8: Swallowed errors
A rejected Promise that nobody handles shows up as a console message at best. A common pattern is a try block around the save only, or a catch that does nothing.
async handleSave(event) {
const { contactId, email } = event.detail;
try {
await updateEmail({ contactId, email });
await refreshApex(this.contacts);
} catch (e) {
// Do something visible. An empty catch hides every failure above.
this.saveError = e;
}
}When the catch shows a message, you can tell a failed save from a failed refresh. If the save fails, the refresh is skipped on purpose, because nothing changed on the server.
A diagnosis order
Work through the causes in this order. It goes from the cheapest check to the most expensive.
The last branch is rare. If the response has the new values and the screen is still old, check that the template reads from the field that holds the wire result, as in contacts.data, and not from a separate copy you made earlier.
Summary of symptoms and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| No second Apex request | Wrong argument, imperative data, or `undefined` result | Pass the full wired result; guard against `undefined` |
| Second request, old values | Refresh ran before the save finished | `await` the save first |
| Second request, old values after awaited save | Query, sharing, or transaction issue | Fix the Apex or data |
| Works once, then stops | Function wire stored a stale result object | Reassign the stored result on every wire call |
| Works until a sort or edit | Wired field was overwritten | Use a getter for derived values |
Exercise
A developer writes this.wiredResult = data; inside a function wire, where data comes from const { data, error } = result;. Later, refreshApex(this.wiredResult) runs after a save and nothing changes. What is wrong, and what is the fix?
Show answer
data is only the array of records, so this.wiredResult is not the object the wire provisioned. refreshApex has no wire to find. The fix is to store the whole argument, this.wiredResult = result;, and keep data in a separate field for the template.
Exercise
After saving, a component shows old values. The Network tab shows a second Apex request after the save, and its response contains the old email. List two causes you would check next, and say why changing the refreshApex call will not help.
Show answer
Check whether the SOQL query selects the changed field and whether the record is cut off by the WHERE clause or LIMIT. Also check whether the update has committed, or whether sharing or field-level security hides the new value from the user. The refresh did its job by making the request, so the component code is fine and the server response is the problem.