最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getStaticPaths throws "TypeError: segment.replace is not a function" - Stack Overflow

programmeradmin3浏览0评论

I'm new to Next.js's getStaticPaths and am now getting this error, having no clue what to do with it.

This is my code (I'm using 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
    };
}

This returns in this error (on every page):

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.

In my console the log is as follows:

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)

I have tried a couple of things.

The reason I wrapped my mappedIds in the silly parse/stringify construction is because of this issue: .js/issues/11993.

Which appears to have been necessary, because when I omit the mappedIds with all the logic from getStaticPaths, and use the same logic in getStaticProps so I can pass mappedIds as a prop, I need to wrap the object in the parse/stringify construction or else it won't work.

Furthermore, once this worked, this allowed me to console.log the object to see what's going on. The result is this:

Which appears to be just formatted in the way I would need it in getStaticPaths.

Does anyone have any idea what is happening behind the surface (and more importantly, how to fix it)?

I'm new to Next.js's getStaticPaths and am now getting this error, having no clue what to do with it.

This is my code (I'm using 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
    };
}

This returns in this error (on every page):

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.

In my console the log is as follows:

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)

I have tried a couple of things.

The reason I wrapped my mappedIds in the silly parse/stringify construction is because of this issue: https://github./vercel/next.js/issues/11993.

Which appears to have been necessary, because when I omit the mappedIds with all the logic from getStaticPaths, and use the same logic in getStaticProps so I can pass mappedIds as a prop, I need to wrap the object in the parse/stringify construction or else it won't work.

Furthermore, once this worked, this allowed me to console.log the object to see what's going on. The result is this:

Which appears to be just formatted in the way I would need it in getStaticPaths.

Does anyone have any idea what is happening behind the surface (and more importantly, how to fix it)?

Share Improve this question edited Oct 15, 2021 at 20:29 juliomalves 50.6k23 gold badges178 silver badges169 bronze badges asked Jan 5, 2021 at 14:55 RobbeoliRobbeoli 4185 silver badges17 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 10

The values in your slug arrays need to be strings. Having numbers is what's triggering the error.

const mappedIds = userIds.map(id => ({ 
    params: { slug: [`${id.userId}`] }
}));
发布评论

评论列表(0)

  1. 暂无评论