@Wire returns undefined for a hard refresh in browser - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Sameera SilvaSameera Silva 

@Wire returns undefined for a hard refresh in browser

I have a LWC component in my home page with the following code
 
import { LightningElement,wire} from 'lwc';
import NAME_FIELD from '@salesforce/schema/Account.Name';
import { getRecord } from 'lightning/uiRecordApi';

export default class Wire extends LightningElement {

    @wire(getRecord,{recordId:'0011y00000GUM8dAAH',fields:NAME_FIELD})
    value;

    renderedCallback(){
        console.log(this.value);
    }
}

when I do a hard reload I am getting the following response in browser console.

User-added image

after clicking on the home tab I am getting the data

User-added image

why is it retruning empty object for the hard refresh?
 
Best Answer chosen by Sameera Silva
Usman AliUsman Ali
Hi Sameera,

@Wire method doesn't respect connectedCallBack() method because there is no order of execution for @wire and connectedcallback method there run asynchronously, if you want to check result is coming with getRecord you have put console log inside @wire function instead of property.

Below is an example. @track opp variable is holding getRecord information same like your value property. You can use below snippet to make sure your record is being loaded or not.
import { LightningElement, api, wire, track } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';

const oppFields = ["Opportunity.SyncedQuoteId", "Opportunity.AccountId"];

export default class LeaseSwap extends LightningElement
{
    @api opportunityId;
    
    @track opp = undefined;

    @wire(getRecord, {recordId : '$opportunityId', fields : oppFields })
    wiredOpp({error, data})
    {
        if(data)
        {
            console.log('UAC: opp ' + JSON.stringify(data));
            this.opp = data;
        }
        else if(error)
        {
            console.log('UAC: error ' + JSON.stringify(error));
        }
    }
}