Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Updating Array Field Hangs in CollectionAfterOperationHook

discord user avatar
denolfe
last year
9

Have you tried wrapping it in a trycatch to see if there is some sort of error maybe?

  • default discord avatar
    dawitt1last year

    I just did but it looks like nothing is thrown. I also tried with a fresh DB but it didn't help.

  • discord user avatar
    denolfe
    last year

    Can you provide the full hook code?



    Looks like you just provided the internals

  • default discord avatar
    dawitt1last year

    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
    }
  • discord user avatar
    denolfe
    last year

    Hmm, makes me wonder if updating the user's projects is causing a recursive loop.



    Can you try updating

    something else

    to rule that out?

  • default discord avatar
    dawitt1last year

    Of course, I'll try!

  • discord user avatar
    denolfe
    last year

    Maybe changing some other prop on the user

  • default discord avatar
    dawitt1last year
    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

    afterChange

    if 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.update

    resolves 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

    resolved

    is never logged.


    Does this give you any idea what could be going on here?

  • default discord avatar
    florianmsftlast year

    hey

    @760857450663641138

    could this be related to the issue I have?

    https://discord.com/channels/967097582721572934/1267772733899407513/1267772733899407513
  • default discord avatar
    dawitt1last year

    Thanks for letting me know, definitely sounds like it could be related!



    @783780529966153749

    /

    @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

    req

    to 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/7352

    I don't quite understand why yet but I'm glad it works.

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.