Get by slug

default discord avatar
kvist__
3 months ago
5

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
    paulpopus
    3 months 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__
    3 months ago

    true - I'll do it that way. Thanks

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.