0

I have a function in LWC which is getting invoked if Wire brings some values from Apex. but issue here i am facing is function is still getting called when data=[]

Logic : Wire

@wire(getRFCt, { crid: '$crid' }) wired({ error, data }) {
        const functionName = 'wired';
        if (error) {
            //Log to print Error
        }
        else if (data) {
             {
                this.ceSler(data);
                console.log("data is here" + JSON.stringify(data));
            }
        }
    }
    
    function
    
    ceSler(data) {
        if (data !== []) {
              /// Logic for Functio
            });
        }
    }
    
    
data is here[{"T_R_R":"ASD-SDF-DFG","Id":"hgjfhgjf"}]
data is here[]

How to invoke the function only when (data is here[{"T_R_R":"ASD-SDF-DFG","Id":"hgjfhgjf"}]) value is present in data currently not sure why it is getting invoked for both. kindly help me out for this.

  • check for length data.length > 0 – User6670 Jul 21 at 8:14
1

Change your condition to:

if(data && data.length) {
  this.ceSler(data);
}

0 is a "falsy" value, so if the array is empty, this condition will not call the method.

| improve this answer | |
0

if(data) evaluates against null value. In this scenario as @sfdcfox suggested you could check null and length. Alternatively In getRFCt() method if there is no valid data make sure return null.

| improve this answer | |

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.