简体   繁体   中英

Getting expected to return a value at the end of the arrow function error

I have a function like this, 在此处输入图像描述

  const isDisplayStaticAndConditionalSwitcher = (fieldNode, window) => {
    const fieldDataSourceCode = fieldNode.getAttribute('DataSourceCode') || [];
    const nodeValues = Object.values(window?.Designer?.nodes); // get the nodes values 
     
    const formDataSourceCode = nodeValues.map((o) => {
      if (o.displayName === 'Form') { return o.props.code; } 
    }).filter((v) => v)[0];

    return fieldDataSourceCode === formDataSourceCode;
  };

I am getting the error, expected to return a value at the end of the arrow function error How should I resolve the issue?

Your lint rules want you to explicitly return undefined:

nodeValues.map((o) => {
  if (o.displayName === "Form") {
    return o.props.code;
  } else {
    return undefined;
  }
});

The lint error is because of if condition inside map function. you need to return the same value or other in case of if condition fails.

Using map, Expectation from map function to return same length of Array.

const formDataSourceCode = nodeValues.map((o) => {
      if (o.displayName === 'Form') { return o.props.code; }  
      // add the return in case if condition fails.
      return o;
    }).filter((v) => v)[0];

Hope this is helpful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM