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 all child rows referring to parent

default discord avatar
eloahsam2 years ago
1

So i have a parent and child table and i want to get the total number children referring to parents and i previously used a afterRead Hook


afterRead: [ (data: any) => { return queryRelations(data, "/api/experiences"); }, ],

and in the function


const get = (url, params) => { const query: any = qs.stringify(params, { addQueryPrefix: true }); return fetch(

${url}${query}

); }; export const queryRelations: any = async (data, route) => { const request: Response = await get(payload.getAdminURL() + route, { where: { // the 'in' operator is used when relations can be more than one category: { in: data.id }, // to add more query constraints use 'or', 'and' operator objects }, }); var result: any = await request; if (result?.docs) { return result?.totalDocs; } if (result.status >= 400) { return console.error(); } return 0; };

the payload in response seems to have changed as docs and total docs is undefined ,



can anyone form payload assist me @jmikrut



i found the answer i can query using

export const queryRelations: any = async (data, route) => { const experiences = await payload.find({ collection: "experiences", where: { // the 'in' operator is used when relations can be more than one category: { equals: data.id }, // to add more query constraints use 'or', 'and' operator objects }, depth: 0, limit: 0, }); if (experiences?.docs) { return experiences?.totalDocs; } return 0; };
    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.