Have you tried wrapping it in a trycatch to see if there is some sort of error maybe?
I just did but it looks like nothing is thrown. I also tried with a fresh DB but it didn't help.
Can you provide the full hook code?
Looks like you just provided the internals
Sure!
const hook: CollectionAfterOperationHook<'projects'> = async ({
args, // arguments passed into the operation
operation, // name of the operation
req, // full express request
result, // the result of the operation, before modifications
}) => {
const { user, payload } = req
if (!user) {
return result
}
if (operation === 'create' && result.creator === user.id) {
const projectId = result.id
console.log('Starting')
console.log({ projectId })
try {
await payload.update({
collection: 'users',
id: user.id,
data: {
projects: [
{
role: 'admin',
project: projectId,
},
],
},
})
} catch (error) {
console.error('An error', error)
}
console.log('resolved')
}
return result
}Hmm, makes me wonder if updating the user's projects is causing a recursive loop.
Can you try updating
something elseto rule that out?
Of course, I'll try!
Maybe changing some other prop on the user
const hook: CollectionAfterOperationHook<'projects'> = async ({
args, // arguments passed into the operation
operation, // name of the operation
req, // full express request
result, // the result of the operation, before modifications
}) => {
const { user, payload } = req
if (!user) {
return result
}
if (operation === 'create' && result.creator === user.id) {
const projectId = result.id
console.log('Starting')
console.log({ projectId })
try {
await payload.update({
collection: 'users',
id: user.id,
data: {
firstName: 'Test name',
},
})
} catch (error) {
console.error('error', error)
}
console.log('resolved')
}
return result
}Still the same problem 🤔
What's weird is that I have an API endpoint on the user collection running the same update code and it works fine.
Same happens using
afterChangeif that helps
I have another collection, "Agencies" to which the user collection holds the same kind of references (inspired by the multi-tenant example). Updating anything from "Agencies" doesn't work either (same problem). Updating fields of other collections works fine in the hook.
Hi
@967118574445547650,
I know it has been a while but I'm taking another look at this and found out that the
payload.updateresolves as expected if I wrap it in a
setTimeout:
const hook: CollectionAfterChangeHook<Project> = async ({
doc, // full document data
req, // full express request
previousDoc, // document data before updating the collection
operation, // name of the operation ie. 'create', 'update'
}) => {
const { user, payload } = req
if (!user) {
return doc
}
if (operation === 'create' && doc.creator === user.id) {
const projectId = doc.id
console.log('Starting')
console.log({ projectId })
try {
setTimeout(async () => {
await payload.update({
collection: 'users',
id: user!.id,
data: {
projects: [
{
role: 'admin',
project: projectId,
},
],
},
})
console.log('resolved')
}, 0)
} catch (error) {
console.error('An error', error)
}
}
return doc
}Without the setTimeout the create operation is stuck and
resolvedis never logged.
Does this give you any idea what could be going on here?
hey
@760857450663641138could this be related to the issue I have?
https://discord.com/channels/967097582721572934/1267772733899407513/1267772733899407513Thanks for letting me know, definitely sounds like it could be related!
/
@967118574445547650: The fix proposed in
@783780529966153749's thread (
https://discord.com/channels/967097582721572934/1267772733899407513/1267772733899407513) indeed fixed the problem for me. I just had to pass the
reqto the update options:
await payload.update({
req,
collection: 'users',
id: user!.id,
data: {
projects: [
{
role: 'admin',
project: projectId,
},
],
},
})This is the related Issue on Github:
https://github.com/payloadcms/payload/issues/7352I don't quite understand why yet but I'm glad it works.
Star
Discord
online
Get dedicated engineering support directly from the Payload team.