Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Get by slug

default discord avatar
kvist__2 years ago
2

Hi



I'm trying to setup payload cms to get content by 'slug', eg

/en-gb/new-items

However, currently I can only look into one collection at a time, and with content pages, login page and category pages, product page etc, I would love to create a setup where I can go look for the doc once, rather than having to manually check all collections - is this possible?



Thanks 🙂



I currently have this endpoint:


endpoints: [
    {
      path: '/:market?/*',
      method: 'get',
      root: true,
      handler: async (req, res, next) => {
        const market = req.params.market;
        const slug = req.params[0];
        const docs = await req.payload.find({
          collection: 'pages',
          locale: market,
          where: {
            slug: {
              equals: market ? market + '/' + slug : slug,
            }
          },
        });
        if (!docs || !docs.docs || !docs.docs.length) {
          return next();
        }
        return res.json(docs.docs[0]);
      },
    }
  ],
  • default discord avatar
    paulpopus2 years ago

    Youre trying to search multiple collections right? I think that's not currently possible due to a mongoose (orm) limitation



    What you can do though is put your

    find

    function into a re-usable async function that takes in a collection parameter, then run it for every collection in your list



    You can get an array of

    find

    promises and resolve them all with

    await Promise.all([])
  • default discord avatar
    kvist__2 years ago

    true - I'll do it that way. Thanks

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.