2

I'm learning to write Lightning Web Components (and also rather a novice at modern javascript).

I'm trying to make the code on the LWC Developer Guide page Handle Errors in Lightning Data Service work. But when my callback is invoked, both data and error are undefined.

What am I doing wrong?

@wire(getRecord, { recordId: '$recordId', fields })
wiredRecord({error, data}) {
    debugger;
}

Chrome developer tools debugger

2 Answers 2

1

It is most likely that your component is not yet constructed and thus you are seeing the default values of the properties for a wired function, which happens to be undefined. If you let the component to be rendered, you should be able to view the values if you have appropriate access to the record.

This is documented in Use the Wire Service to Get Data:

The property is assigned a default value after component construction and before any other lifecycle event. The default value is an object with data and error properties of undefined

...

The wire service provisions the function an object with error and data properties, just like a wired property.

1
  • Thank you Jayant! I just needed to ignore the first invocation and debug the second.
    – Bo Laurent
    Commented May 2, 2019 at 20:16
1

The wire service first provisions the shape of the value: { data: undefined, error: undefined }. This enables direct use of wired properties in the template to test for data and error. Eg <template if:true={wiredProperty.data}>. This is why you see said value provided to your wired function.

The wire service will invoked your method a second time, just give it time. You can check the network tab of Chrome devtools (or equivalent in your preferred browser) to see the status of the network request triggered by your @wire.

In terms of code structure:

  1. You should check for data and error and respond accordingly. See lwc-recipes' deleteRecord.js for an example.
  2. The Lightning Data Service @wire adapters correspond to UIAPI resources. Check out that doc site for the structure of the errors. The docs you reference are based on those resources' error structures.

You must log in to answer this question.

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