我正在 SSR 模式下使用 Nuxt,并希望為多個(gè)路線/數(shù)據(jù)集構(gòu)建動(dòng)態(tài)站點(diǎn)地圖。我現(xiàn)在面臨的問(wèn)題是 async/await 函數(shù)只允許“data”作為變量。使用“post”作為變量的相同函數(shù)會(huì)導(dǎo)致“地圖函數(shù)不存在”這是我的 Nuxt.config.js 文件中的內(nèi)容 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' }我正在尋找的結(jié)果輸出應(yīng)采用如下格式:[ '/locations/location-one', '/locations/location-two', '/locations/location-three', '/posts/post-one', '/posts/post-two', '/posts/post-three',]我收到的錯(cuò)誤:Cannot read property 'map' of undefined它發(fā)生在這條線上:posts = posts.map((post) => `/posts/${post.slug}`)所以在我看來(lái),它不接受“posts”作為其自己的等待函數(shù)的有效變量。當(dāng)?shù)谝粋€(gè)調(diào)用被注釋掉并且使用“數(shù)據(jù)”而不是“帖子”時(shí),該調(diào)用工作正常
從多個(gè)數(shù)據(jù)源構(gòu)建 Nuxt 站點(diǎn)地圖
蝴蝶不菲
2023-08-18 10:17:40