繁体   English   中英

期望在箭头 function 错误的末尾返回一个值

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

我有一个像这样的 function, 在此处输入图像描述

  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;
  };

我收到错误, expected to return a value at the end of the arrow function error我应该如何解决这个问题?

您的 lint 规则希望您显式返回 undefined:

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

lint 错误是由于map function 内部的if条件造成的。 如果条件失败,您需要返回相同的值或其他值。

使用 map,期望 map function 返回相同长度的数组。

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];

希望这会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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