[SalesForce] Data not returned by wire service

I am unable to get data using getRecord method if wired to a function. The same call work when it is wired to property. I have given the code below. When wired to function, error and data (in wiredAccount function) are coming back as undefined.

Wired to method (not working)

@wire(getRecord, { recordId: '$clientId', fields: accountFields })
wiredAccount({ error, data }) {
    console.log('in wiredAccount');
    if (data) {
        this.account = data;
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.account = undefined;
    }
}

Wired to property (working)

@wire(getRecord, { recordId: '$clientId', fields:accountFields })
account;

Best Answer

The wire service first provisions the shape of the value: { data: undefined, error: undefined }. This enables wiring of properties and their direct use 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.

As for why it's not being invoked a second time with the data or error, I recommend checking the values of this.clientId and this.accountFields. Are either undefined? Per https://developer.salesforce.com/docs/component-library/documentation/lwc/data_wire_service_about, Properties in the adapterConfig object can’t be undefined. If a property is undefined, the wire service doesn’t provision data. Try setting a breakpoint (eg with debugger) in connectedCallback() and inspecting the values.