Javascript 从多个数据源构建Nuxt站点地图

Javascript 从多个数据源构建Nuxt站点地图,javascript,arrays,vue.js,async-await,nuxt.js,Javascript,Arrays,Vue.js,Async Await,Nuxt.js,我正在SSR模式下使用Nuxt,希望为多个路由/数据集构建一个动态站点地图 我现在面临的问题是,async/await函数只允许“data”作为变量。与变量“post”相同的函数导致“映射函数不存在” 这是我的Nuxt.config.js文件中的内容 sitemap: { hostname: "https://example.com", routes: async () => { let { data } = await axios.ge

我正在SSR模式下使用Nuxt,希望为多个路由/数据集构建一个动态站点地图

我现在面临的问题是,async/await函数只允许“data”作为变量。与变量“post”相同的函数导致“映射函数不存在”

这是我的Nuxt.config.js文件中的内容

  sitemap: {
    hostname: "https://example.com",
    routes: async () => {
      let { data } = await axios.get('https://api.example.com/api/v1/locations');
      data = data.data.map((loc) => `/locations/${loc.slug}`);
      console.log(data);

      let { posts } = await axios.get('https://cms.example.com/wp-json/wp/v2/posts');
      posts = posts.map((post) => `/posts/${post.slug}`);
      console.log(posts);

      data.concat(posts);

      return data
    },
    path: '/sitemap.xml'
  }
我要查找的结果输出的格式应如下所示:

[
  '/locations/location-one',
  '/locations/location-two',
  '/locations/location-three',
  '/posts/post-one',
  '/posts/post-two',
  '/posts/post-three',
]
我得到的错误是:

Cannot read property 'map' of undefined
它发生在这条线上:

posts = posts.map((post) => `/posts/${post.slug}`)
所以在我看来,它不接受“posts”作为其自身等待函数的有效变量


当第一个调用被注释掉并且使用“data”而不是“posts”时,该调用可以正常工作。

您的数组连接必须如下所示:

data.concat(posts);
(见附件)


push()


(请参阅)

您的结构化响应错误:

替换:

let{posts}=。。。
作者:


因为Axios总是返回一个“data”属性,所以您只需将其重命名为“posts”。

自v2以来,模块不再使用“generate:false”选项。您可以删除它以避免警告。谢谢,我已经删除了“generate:false”。仍然会遇到相同的错误“无法读取未定义的属性“map”。非常感谢,我已经编辑了这篇文章。主要问题是为什么“posts”(特别是posts上的map函数)不被识别,而“data”会被识别,以及如何处理这两个问题,因为它们不能同时被分配为“data”。这就解决了问题,然后我使用了
let routes=[].concat(farms).concat(posts)
返回路线
let { data: posts } = ...