#How to populate doc in hook?

6 messages · Page 1 of 1 (latest)

hexed sedge
#

Hello guys, I am using afterChange hook in my Posts collection and I need to access author of that collection in my hook. But doc.author returns just the id of author. Is there a way to populate fields in doc in hooks?
I tried defaultPopulate on Posts collection, but that does not work.

Thanks.

fiery lantern
#

Hello!

#

You could make a database query to the author collection, passing the ID to get all the information of that author. Otherwise, maybe adding the parameter depth: '2' might be enough.

#

afterChange: [async ({ doc, req }) => {
const authorData = await req.payload.findByID({
collection: 'authors',
id: doc.author,
})
}],

hexed sedge
#

Hello @fiery lantern , thank you. I was thinking about making separate query to author, but I thought there is a way to populate doc directly. :/ When the post is in status "draft", all fields are populated somehow, but not when published.