#Dynamic routing with parameters and paginate

16 messages · Page 1 of 1 (latest)

raven zephyr
#

I'm trying to create an astro page using dynamic routing and pagination. I named the file as [galleryID]-[page].astro and this is the code https://pastebin.com/DWRFGbd0

Astro returns me a TypeError with "Expected "galleryID" to be a string", with this stacktrace

 error   Expected "galleryID" to be a string
  File:
    /home/sirio/Progetti/AASApp/node_modules/path-to-regexp/dist/index.js:237:19
  Code:
    236 |             var typeOfMessage = repeat ? "an array" : "a string";
> 237 |             throw new TypeError("Expected \"".concat(token.name, "\" to be ").concat(typeOfMessage));
          |                   ^
      238 |         }
      239 |         return path;
      240 |     };
  Stacktrace:
TypeError: Expected "galleryID" to be a string
    at Object.generate (/home/sirio/Progetti/AASApp/node_modules/path-to-regexp/dist/index.js:237:19)
    at file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/core/render/paginate.js:29:52
    at Array.map (<anonymous>)
    at paginateUtility (file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/core/render/paginate.js:21:48)
    at Module.getStaticPaths (/home/sirio/Progetti/AASApp/src/pages/gallery/[galleryID]-[page].astro:14:10)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async callGetStaticPaths (file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/core/render/route-cache.js:25:17)
    at async getParamsAndProps (file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/core/render/params-and-props.js:11:23)
    at async matchRoute (file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/vite-plugin-astro-server/route.js:32:7)
    at async run (file:///home/sirio/Progetti/AASApp/node_modules/astro/dist/vite-plugin-astro-server/request.js:51:28)

How can I retrieve galleryID to be used for photoset_id parameter in fetch url?

stiff pineBOT
#
No-one around right now?

It looks like no-one has responded to your question yet. People might not be available right now or don’t know how to answer your question. Want an answer while you wait? Try asking our experimental bot in #1095492539085230272.

raven zephyr
#

I asked to the bot and it says to change file structure as gallery/[galleryID]/page/[page].astro, but I have the same problem 🤷‍♂️

raven zephyr
#

any help?

proven turtle
#

So what url format do you want? /[galleryID]-[page] like /0-mypage?

#

In your pastebin, you probably need to map over your data (allPhotos), I don't what shape it has

raven zephyr
proven turtle
#

I see, I'll give it a try later today!

raven zephyr
#

I tried your solution, with a path like gallery/[galleryID]/[page].astro. If I run netlify dev and I click on a gallery it gives me a 404 and in console this error

08:53:02 PM [serve]    404         /gallery/72177720310416326/1.htm
08:53:02 PM [getStaticPaths] A `getStaticPaths()` route pattern was matched, but no matching static path was found for requested path `/gallery/72177720310416326/1/`.

Possible dynamic routes being matched: src/pages/gallery/[galleryID]/[page].astro.
#

If I run npm run build it gives this error

▶ src/pages/gallery/list-[page].astro
 error   Cannot read properties of undefined (reading 'photoset')
  File:
    /home/sirio/Progetti/AASApp/node_modules/astro/dist/core/render/route-cache.js:25:17
  Code:
    24 |   let staticPaths = [];
    > 25 |   staticPaths = await mod.getStaticPaths({
         |                 ^
      26 |     // Q: Why the cast?
      27 |     // A: So users downstream can have nicer typings, we have to make some sacrifice in our internal typings, which necessitate a cast here
      28 |     paginate: generatePaginateFunction(route),
  Stacktrace:
TypeError: Cannot read properties of undefined (reading 'photoset')
    at file:///home/sirio/Progetti/AASApp/dist/chunks/pages/list-_page__d69c07e8.mjs:13:63
   [other lines of stacktrace]
#

And it's named gallery/list-[page].astro

raven zephyr
#

Data Is fetching right, anchor link are built right.
If I try api call in postman or insomnia It works

proven turtle