Update other collections with hook

default discord avatar
buzzworthy7
last month
1

I have a collection called 'Votes' where a user will vote on another location called 'Locations'. Each Vote will have a locationId and UserId. Would it be possible to update a total tally of votes in the related location collection after a vote document is created using a Hook? I'm not qutie sure if they're only intended for updating document in the same collection or not.



I'm thinking Sibling Data is what I need to use here:



import type { FieldHook } from 'payload/types';



// Field hook type is a generic that takes three arguments:


// 1: The document type


// 2: The value type


// 3: The sibling data type



type ExampleFieldHook = FieldHook<ExampleDocumentType, string, SiblingDataType>;



const exampleFieldHook: ExampleFieldHook = (args) => {


const {


value, // Typed as

string

as shown above


data, // Typed as a Partial of your ExampleDocumentType


siblingData, // Typed as a Partial of SiblingDataType


originalDoc, // Typed as ExampleDocumentType


operation,


req,


} = args;



// Do something here...



return value; // should return a string as typed above, undefined, or null


}

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.