TransWikia.com

Call the function if there is values returned by the wire method

Salesforce Asked by Santosh on October 3, 2021

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.

2 Answers

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.

Answered by Madhu Bh on October 3, 2021

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.

Answered by sfdcfox on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP