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.

after create hook not finding document (payload beta.39)

default discord avatar
ratatom9 months ago
1

For my Stories collection, text-to-speech is generated in a hook after creation.


When deployed on Vercel,

req.payload.update

throws a 404.

updateOptions

holds valid values. However, the update method apparently does not find the newly created document. I've also tested

req.payload.findById

for the new document, which also throws a 404. If I search for another Id, it works 🤷‍♂️



Any ideas or working examples how to update a fresh document?



import type { CollectionConfig } from 'payload/types'

export const Stories: CollectionConfig = {
  slug: 'stories',
  admin: {
    useAsTitle: 'title',
  },
  access: {
    create: () => true,
    update: () => true,
  },
  fields: [
    {
      name: 'title',
      type: 'text',
      required: true,
    },
    {
      name: 'storyText',
      type: 'textarea'
    },
    {
      name: 'audio',
      type: 'upload',
      relationTo: 'audio'
    }
  ],
  hooks: {
    afterOperation: [
      async ({
        args,
        operation,
        req,
        result,
      }) => {
        if (operation !== "create") return result;

        
        // [... generate audio and make formData ...]


        const audioCollectionResponse = await fetch(audioEndpointUrl, {
          method: 'POST',
          body: formData,
        });

        const data = await audioCollectionResponse.json();
        const audioId = data.doc.id;
        const storyId = result.id;

        const updateOptions = {
          collection: 'stories',
          id: storyId,
          data: {
            audio: audioId,
          },
        };

        const updateResponse = await req.payload.update(updateOptions);

        return result;
      }
    ]
  }
}


Here's the Vercel Log:



[13:22:15] ERROR: o: Not Found
at T (/var/task/.next/server/chunks/4433.js:102:33647)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async f.collections.hooks.afterOperation (/var/task/.next/server/app/(payload)/api/[...slug]/route.js:5:313)
at async /var/task/.next/server/chunks/4433.js:102:36081
at async i (/var/task/.next/server/chunks/4433.js:102:36000)
at async E (/var/task/.next/server/chunks/4433.js:102:13899)
at async Object.t$ [as create] (/var/task/.next/server/chunks/5828.js:6:77041)
at async /var/task/.next/server/chunks/5828.js:6:118121
at async /var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:33006
at async eS.execute (/var/task/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:14:24085)


after create hook not finding document (payload beta.39)



Bumped version to beta.39


This did not change anything.



Exactly this was it. Solved, thanks! 💚

  • default discord avatar
    ritsu045510 months ago

    always pass

    req

    into operations within hooks because it holds current transaction contxext



    it could work locally if you aren't using mongodb with replicaSet

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.