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) {
                console.log("data is here" + JSON.stringify(data));
    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 '20 at 8:14

Change your condition to:

if(data && data.length) {

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


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.

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.