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] [31mERROR[39m: [36mo: 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)[39m
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! 💚
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
Discord
online
Get dedicated engineering support directly from the Payload team.