【问题标题】:Getting expected to return a value at the end of the arrow function error期望在箭头函数错误的末尾返回一个值
【发布时间】:2022-01-20 15:19:39
【问题描述】:

我有这样的功能,

  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 我应该如何解决这个问题?

【问题讨论】:

  • 这不是错误,而是 linter 警告。您是否阅读了您正在使用的 linting 规则的说明?
  • 你可能想写nodeValues.find(o => o.displayName === 'Form')?.props.code

标签: javascript ecmascript-6 arrow-functions


【解决方案1】:

lint 错误是因为 map 函数内部的 if 条件。如果条件失败,您需要返回相同的值或其他值。

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

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

希望这有帮助。

【讨论】:

    【解决方案2】:

    您的 lint 规则希望您明确返回 undefined:

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

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 2021-11-09
      • 2021-01-15
      • 2021-11-22
      • 2022-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多