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

Nuxt Sitemap Module生成sitemap.xml报dynamicRoutes.map is not a function错误

报错原因

这个报错的核心原因是Nuxt Sitemap Module要求routes配置项返回路由字符串数组,但你当前直接返回了axios.get的请求响应对象,而非接口返回的slug数组,模块尝试对响应对象调用map方法自然会失败。

解决方法

修改sitemap配置里的routes字段,改为异步函数,等待请求完成后取出响应体中的数据返回即可:

sitemap: {
    hostname: process.env.PH_WEB_APP_BASE_URL || 'http://localhost:3000/',
    gzip: true,
    exclude: ExcludedPaths,
    cacheTime: 1000 * 60 * 15,
    defaults: {
      changefreq: 'daily',
      priority: 1,
      lastmod: new Date()
    },
    // 改为async异步函数
    routes: async () => {
      const baseUrl = process.env.PH_WEB_APP_BASE_URL || 'http://localhost:3000/';
      // 等待请求返回,取出响应数据
      const res = await axios.get(`${baseUrl}api/sitemap-slugs/`);
      const slugs = res.data;
      // 如果你的动态路由前缀是/product/之类的,需要自行拼接路由路径
      return slugs.map(slug => `/product/${slug}`);
      // 如果接口返回的slug已经是完整路由路径,可以直接return slugs
    }
  },
额外排查点
  • 如果修改后还是报错,先打印res.data确认接口返回结构,若后端返回的是{code: 200, data: [...], msg: 'success'}这类封装结构,需要取res.data.data作为slug数组
  • 确认返回的数组中的每一项都是字符串格式的路由路径,没有嵌套对象或者其他类型值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 03:24:00