Hi everyone,
I’m working on a project using PayloadCMS and want to update global data (navigation) after saving or modifying a document in the Pages collection. I’m using the CollectionAfterChangeHook, but I’m having some issues with implementing the global data update.
Here’s my current code:
import { CollectionAfterChangeHook } from "payload";
import { Page } from "@/payload-types";
export const updateNavigation: CollectionAfterChangeHook<Page> = async ({
doc,
req: { payload },
}) => {
const navigation = await payload.findGlobal({
slug: "navigation",
});
await payload.updateGlobal({
slug: "navigation",
data: {
...navigation,
updatedAt: new Date().toISOString(),
},
});
return doc;
};
My questions:
- Is the code above correct for updating globals in PayloadCMS?
- How can I properly use payload.findGlobal and payload.updateGlobal to update the navigation data?
- What’s the best way to pass data from the modified Page (e.g., adding a link to the new page in navigation)?