0

I have two js methods in my js file.

One methods fetches a set of fields from an particular object, using an apex class method.

 @wire(getSObjectFieldNames , { sObjectName : 'Contact' })
sObjectFieldNames({ error, data }) {
    if (data) {
        this.sObjectFieldDetails  = data;
        for(let fieldLabel in data){
            this.mapOfFieldLabelsWithAPI.push({fieldName:fieldLabel, label:data[fieldLabel]});
            this.columns = this.mapOfFieldLabelsWithAPI;
        }
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.data  = undefined;
    }
}

The second method should pass the set of fields - fetched from the previous method as parameter to another apex class - method and should fetch a record list having those field details

 @track error;
@track sObjectList ;

@wire(setSObjectRecordList , { sObjectName : 'Contact',
                               sObjectFieldList :  this.columns})    
sObjectRecordList({error, data }) {
    console.log( "*********");
    if (data) {
        console.log("__________"+JSON.stringify(this.columns));
        this.sObjectList = data;
    } else if (error) {
        this.error = error;
    }
}

My problem here is that for the second method this.columns is coming as undefined, so the second method is throwing an error :

    **Cannot read property 'columns' of undefined**
2

Wires must be written to receive dynamic properties. The second wire should be called like:

@wire(setSObjectRecordList , { sObjectName : 'Contact',
                               sObjectFieldList :  "$columns"})    
...

A parameter given as "$name" maps to this.name automatically. You may need to set the columns property as tracked depending on just how you manage the property content across your entire LWC.

NB: if at all possible you should try to use the uiRecordApi and related object info features since these behave well in their sharing of cached information and the propagation of tracked updates across the entire Lightning page.

2
  • Yes, I totally missed, that and kept thinking my issue was somewhere else. Also, as far as I know uiRecordApi - getRecord method can just fetch a single record. But my requirement is to fetch a list of Contact Records depending on the AccountId filter. Is that possible using uiRecordApi? @PhilW – Madhurima Dec 28 '20 at 4:17
  • It is true this service only supports a single record (which is annoying given that the underlying REST API supports multi-record fetch). – Phil W Dec 28 '20 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.