You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

修复Array.prototype.map()箭头函数末尾未返回值的ESLint报错

问题描述

编写站点sitemap路由生成逻辑时,使用Array.prototype.map()遍历axios请求返回的资源列表,通过多层else if分支判断,仅对resourceType为brand、product、category、document四类资源返回对应的sitemap路由配置对象,未设置else兜底分支(业务要求严格仅展示上述指定类型资源)。当前ESLint在箭头函数=>位置抛出规则错误:

Array.prototype.map() expects a value to be returned at the end of arrow function

网传在兜底分支返回null的方案无法满足过滤非目标类型资源的需求,需要给出符合ESLint规范且满足业务要求的正确写法。

问题原代码
routes: async () => {
  const apiURL = process.env.BASE_URL
  const response = await axios.get(`${apiURL}/urls`)
  const { data: resources } = response.data
  const urlModule = resources.map((resource) => {
    const { resourceType } = resource

    if (resourceType === 'brand') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.9,
      }
    } else if (resourceType === 'product') {
      return {
        url: `/${resource.url}`,
        changefreq: 'monthly',
        priority: 0.8,
      }
    } else if (resourceType === 'category') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.7,
      }
    } else if (resourceType === 'document') {
      return {
        url: `/${resource.url}`,
        changefreq: 'weekly',
        priority: 0.6,
      }
    }
  })

  return [
    {
      url: '/',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account/order-history',
      changefreq: 'daily',
      priority: 1,
    },
    ...urlModule,
  ]
},
错误原因
  • Array.prototype.map()的设计规则是遍历每一项必须返回值,最终生成和原数组长度完全一致的新数组。当前逻辑中非四类目标资源的分支没有返回值,直接触发ESLint的数组回调返回值校验规则。
  • 直接在兜底分支返回null/undefined再展开的话,无效值会被带入最终路由数组,不符合业务仅保留指定类型资源的要求。
正确实现方案

核心思路是拆分遍历职责:先用filter筛出符合类型要求的资源,再对筛选后的数组执行map生成路由配置,从根源避免无效分支、无效值问题。

routes: async () => {
  const apiURL = process.env.BASE_URL
  const response = await axios.get(`${apiURL}/urls`)
  const { data: resources } = response.data
  const validTypes = ['brand', 'product', 'category', 'document']
  const urlModule = resources
    // 第一步:过滤,仅保留指定类型的资源,从源头剔除无效项
    .filter(resource => validTypes.includes(resource.resourceType))
    // 第二步:遍历有效资源生成路由配置,所有项必然命中分支,无漏返回问题
    .map((resource) => {
      const { resourceType, url } = resource
      const baseConfig = {
        url: `/${url}`,
        changefreq: 'weekly'
      }
      switch (resourceType) {
        case 'brand':
          return { ...baseConfig, priority: 0.9 }
        case 'product':
          return { ...baseConfig, changefreq: 'monthly', priority: 0.8 }
        case 'category':
          return { ...baseConfig, priority: 0.7 }
        case 'document':
          return { ...baseConfig, priority: 0.6 }
      }
    })

  return [
    {
      url: '/',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account',
      changefreq: 'daily',
      priority: 1,
    },
    {
      url: '/account/order-history',
      changefreq: 'daily',
      priority: 1,
    },
    ...urlModule,
  ]
},
方案优势
  • 完全符合ESLint校验规则:map遍历的所有项都是提前校验过的目标类型,每一次遍历都有明确返回值,不会触发回调缺返回值的报错
  • 满足业务过滤要求:非指定类型的资源在filter阶段就被剔除,不会进入最终路由列表
  • 逻辑可读性更强:过滤、格式转换两个职责拆分到不同遍历方法中,代码意图清晰
  • 维护成本更低:抽离了公共配置项,新增/修改资源类型时不需要重复写相同的配置代码

内容的提问来源于stack exchange,提问作者Pianoc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 12:12:09