Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:
#After change hook "doc" does not have object reference
1 messages · Page 1 of 1 (latest)
This is the code:
`import payload from 'payload';
import { CollectionAfterChangeHook } from 'payload/types';
// Assigns a user ID to object created.
export const addProjectToOrganization: CollectionAfterChangeHook = async ({
doc, // full document data
req, // full express request
previousDoc, //Previous document
operation, // collection operation, i.e 'create', 'update'
}) => {
// Only on create operations
if (operation !== 'create') {
return;
}
const organization = await payload.find({
collection: 'organizations',
where: {
id: doc.organization,
},
});
let organizationProjects = [];
if (organization.docs[0].hasOwnProperty('projects')) {
console.log('has projects');
organizationProjects = organization.docs[0].projects;
console.log([...organizationProjects, doc]);
}
await payload.update({
collection: 'organizations',
id: doc.organization,
data: {
projects: [...organizationProjects, doc],
},
});
return doc; // return modified operation arguments as necessary
};`