【问题标题】:NextJS: "TypeError: Cannot read property 'toLowerCase' of undefined"NextJS:“TypeError:无法读取未定义的属性‘toLowerCase’”
【发布时间】:2021-03-15 14:30:10
【问题描述】:

使用getStaticPaths()getStaticProps() 时出现错误NextJS: "TypeError: Cannot read property 'toLowerCase' of undefined"

【问题讨论】:

    标签: next.js server-side-rendering getstaticprops getstaticpaths ssg


    【解决方案1】:

    问题出在 getStaticPaths() 我直接返回一个字符串数组作为路径:

    代码错误

    export const getStaticPaths = async () => {    
     ...
    
        return {
          paths: ['product1','product2','product3'], //WRONG
          fallback: 'blocking'
        }
    }
    

    解决方案是以不同的结构返回路径数组:

    正确代码

    export const getStaticPaths = async () => {    
     ...
    
        return {
          paths: [
              {'params': {myPageSlug: 'product1'}},
              {'params': {myPageSlug: 'product2'}},
              {'params': {myPageSlug: 'product3'}},
          ], //OK
          fallback: 'blocking'
        }
    }
    

    myPageSlug是命名页面文件时使用的slug,例如:pages/[myPageSlug].tsx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-12
      • 2021-03-22
      • 2021-04-08
      • 2018-11-30
      相关资源
      最近更新 更多