【发布时间】:2021-03-15 14:30:10
【问题描述】:
使用getStaticPaths() 和getStaticProps() 时出现错误NextJS: "TypeError: Cannot read property 'toLowerCase' of undefined"
【问题讨论】:
标签: next.js server-side-rendering getstaticprops getstaticpaths ssg
使用getStaticPaths() 和getStaticProps() 时出现错误NextJS: "TypeError: Cannot read property 'toLowerCase' of undefined"
【问题讨论】:
标签: next.js server-side-rendering getstaticprops getstaticpaths ssg
问题出在 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
【讨论】: