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]);
},
}
],
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([])
true - I'll do it that way. Thanks
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.