Rob*_*oli 4 javascript reactjs next.js
我是 Next.js 的新手getStaticPaths
,现在收到此错误,不知道如何处理它。
这是我的代码(我正在使用query
from serverless-mysql
):
export async function getStaticPaths() {
const userIds = await query(/* sql */`
SELECT userId FROM user_table_v3test
`);
const mappedIds = JSON.parse(JSON.stringify(userIds.map(id => ({ params: { slug: [id.userId]}}))));
return {
paths: [
{ params: { slug: [] } },
...mappedIds
],
fallback: false
};
}
Run Code Online (Sandbox Code Playgroud)
在此错误中返回(在每个页面上):
Server Error
TypeError: segment.replace is not a function
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Run Code Online (Sandbox Code Playgroud)
在我的控制台中,日志如下:
Uncaught at escapePathDelimiters (file://C:\Users\robbert.olierook\Documents\pom_v1\node_modules\next\dist\next-server\lib\router\utils\escape-path-delimiters.js:2:55)
at Array.map (<anonymous>)
at <unknown> (file://C:\Users\robbert.olierook\Documents\pom_v1\node_modules\next\dist\build\utils.js:21:1117)
at Array.forEach (<anonymous>)
at <unknown> (file://C:\Users\robbert.olierook\Documents\pom_v1\node_modules\next\dist\build\utils.js:21:502)
at Array.forEach (<anonymous>)
at buildStaticPaths (file://C:\Users\robbert.olierook\Documents\pom_v1\node_modules\next\dist\build\utils.js:17:1252)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
Run Code Online (Sandbox Code Playgroud)
我尝试过一些事情。
我将我的代码包裹在愚蠢的解析/字符串化构造中的原因mappedIds
是因为这个问题:
https: //github.com/vercel/next.js/issues/11993。
这似乎是必要的,因为当我省略mappedIds
中的所有逻辑getStaticPaths
,并使用相同的逻辑getStaticProps
以便我可以mappedIds
作为道具传递时,我需要将对象包装在解析/字符串化构造中,否则它不会工作。
此外,一旦成功,我就可以通过 console.log 该对象来查看发生了什么。结果是这样的:
这似乎只是按照我需要的方式格式化的getStaticPaths
。
有谁知道表面背后发生了什么(更重要的是,如何解决它)?
jul*_*ves 10
数组中的值slug
必须是字符串。有数字是触发错误的原因。
const mappedIds = userIds.map(id => ({
params: { slug: [`${id.userId}`] }
}));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4285 次 |
最近记录: |