Get all child rows referring to parent

default discord avatar
eloahsam
2 months 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; };
    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.