【问题标题】:getStaticPaths array.map isn't a function when deploying to Vercel [duplicate]部署到 Vercel 时,getStaticPaths array.map 不是一个函数 [重复]
【发布时间】:2022-01-11 12:07:09
【问题描述】:

代码如下,开发服务器工作正常,没有错误,但是当我尝试部署站点时出现此错误。

> Build error occurred
TypeError: artists.map is not a function
    at getStaticPaths (/vercel/path0/.next/server/pages/artists/[slug].js:106:24)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async buildStaticPaths (/vercel/path0/node_modules/next/dist/build/utils.js:498:31)
    at async /vercel/path0/node_modules/next/dist/build/utils.js:641:119
    at async Span.traceAsyncFn (/vercel/path0/node_modules/next/dist/trace/trace.js:74:20) {
  type: 'TypeError'
}
export async function getStaticProps({ params }) {
  const siteSettings = await fetcher("http://localhost:3000/api/settings");
  const artists = await fetcher(
    `${process.env.URL}/api/artists/${params.slug}`
  );
  const artist = artists.allArtist[0];

  return {    
    props: {    
      siteSettings,    
      artist,    
    },    
  };    
} 

export async function getStaticPaths() {    
  const artists = await fetch(`${process.env.URL}/api/artists`);    
     
  return {                                                                                                           
    paths: artists.map((artist) => {                                                                                 
      return {    
        params: {    
          slug: artist.slug.current,    
        },    
      };    
    }),    
    fallback: false,    
  };    
}    

我将如何解决此错误,正如我所说,它在开发服务器中运行良好,没有包含任何错误的控制台日志。

任何帮助将不胜感激。

【问题讨论】:

  • 愚蠢的问题 - 但是否为 prod env 设置了 URL 环境变量?
  • 检查浏览器开发工具的网络选项卡以查看对 /artists 的请求并检查服务器地址是否正确。我也猜想 URL env 没有设置。
  • @adrian 是的,我确实设置了生产环境变量
  • @Andreas Jagiella 我什至无法检查浏览器工具,因为它在 Vervel 的构建期间崩溃了。我的本地主机环境没有错误
  • 这是否回答了您的问题:Fetch error when building Next.js static website in production?您不应从getStaticProps/getStaticPaths 中的内部 API 路由中获取。相反,直接导入 API 路由中使用的逻辑。

标签: javascript reactjs next.js


【解决方案1】:

我尝试直接在 getStaticProps 函数中编写 API 路由逻辑,这修复了错误 @juliomalves 还发布了此链接,建议类似的答案:Fetch error when building Next.js static website in production

感谢您的帮助。

苹果机

【讨论】:

  • 您的问题顶部应该有一个框,要求您确认问题是否重复。使用它而不是发布仅指向其他地方的答案。
猜你喜欢
  • 2021-04-05
  • 2022-07-09
  • 2023-02-25
  • 2022-01-02
  • 1970-01-01
  • 2022-01-25
  • 2020-11-06
  • 1970-01-01
  • 2022-10-07
相关资源
最近更新 更多