#afterChangeHook req.payload Not found

1 messages · Page 1 of 1 (latest)

glacial vessel
#

Hi everyone I have an issue.

Looking into the documentation I see this piece of code:

import type { CollectionConfig } from 'payload'

const MyCollection: CollectionConfig = {
  slug: 'slug',
  hooks: {
    afterChange: [
      async ({ context, doc, req }) => {
        // return if flag was previously set
        if (context.triggerAfterChange === false) {
          return
        }
        await req.payload.update({
          collection: contextHooksSlug,
          id: doc.id,
          data: {
            ...(await fetchCustomerData(data.customerID)),
          },
          context: {
            // set a flag to prevent from running again
            triggerAfterChange: false,
          },
        })
      },
    ],
  },
  fields: [
    /* ... */
  ],
}

However, when I try and run req.payload.update it returns an error "NotFound: Not Found" which is absolutely confusing.

I am aiming to create a afterChangeHook for videos, where I can use my custom made FfmpegService to convert videos into smaller quality for progressive video loading. Conversion is successful and I want to save the converted video data into Media collection, but for some reason req.payload.update throws that error.

I have attached docker log, and to translate from Croatian for convenience:

[FfmpegService] Video successfuly converted
[Video Hook] Updating Document
[Video Hook] ❌ Error occured: NotFound: Not Found

dusty pikeBOT
glacial vessel
#

Here's the code in question:

import { FfmpegService } from '@/utilities/FfmpegService'
import path from 'path'
import { CollectionAfterChangeHook } from 'payload'

export const convertVideoAfterUpload: CollectionAfterChangeHook = async ({ doc, req, context }) => {
  const mimetype = doc?.mimeType || doc?.mimetype || ''
  if (!mimetype.startsWith('video/')) {
    console.log(`[VideoHook] Preskačem jer nije video: ${mimetype}`)
    return doc
  }

  if (context?.triggerAfterChange === false) {
    console.log(`[VideoHook] Preskačeno jer je triggerAfterChange === false`)
    return doc
  }

// CONVERSION OCCURS HERE, JUMPING TO NEXT SECTION

// Document update
    console.log('[VideoHook] Ažuriram dokument...')
    await req.payload.update({
      collection: 'media',
      id: String(doc.id),
      overrideAccess: true,
      context: { triggerAfterChange: false },
      data: {
        convertedVersions: converted,
        thumbnailFilename: thumbnailFilename || undefined,
      },
    })

    console.log('[VideoHook] Konverzija završena uspješno.') // success message
    return doc
  } catch (error) {
    console.error(`[VideoHook] ❌ Došlo je do greške:`, error)
    return doc
  }
}

tl;dr ffmpeg service works, but not updating data into the database.

Any help or clarity about this situation would be great. Thank you in advance.

glacial vessel
#

Yeah, racing conditions. Solved by using: