我正在 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”作為其自己的等待函數的有效變量。當第一個調用被注釋掉并且使用“數據”而不是“帖子”時,該調用工作正常
從多個數據源構建 Nuxt 站點地圖
蝴蝶不菲
2023-08-18 10:17:40