#Having issues with using payload.update()

3 messages · Page 1 of 1 (latest)

unkempt fox
#

Hi,

I have a SiteUsers collection that has an email, roles and a data relationship field. Upon creating a user, I'd like to create another entry in either Organizations or Candidates collection with the email from the SiteUsers collection and update the data field with the newly created profile.

I achieved part 1 of this by using the payload.create() function.

However part 2 is where I'd like to update the data relationship field to include the ID of the created item.

import type { AfterChangeHook } from 'payload/dist/collections/config/types'

export const createProfileAfterCreate: AfterChangeHook = async ({
  doc,
  req,
  req: { payload, body = {}, res },
  operation,
}) => {
  if (operation === 'create' && doc.id) {
    const { email, role } = body

    // Create a profile for the user
    const profile = await payload.create({
      collection: role,
      data: {
        email: email,
      }
    })

    if (profile) {

      console.log('doc', doc)           // works
      console.log('profile', profile)   // works

      // Assign the profile to the user - not working
      await payload.update({
        collection: 'site-users',
        id: doc.id,
        data: {
          data: {
            value: profile.id,
            relationTo: role
          }
        },
        depth: 0
      })

    }

  }
  return doc
}

I logged the doc and profile (see screenshots) and I get both the IDs of the SiteUsers collection item and the Organizations/Candidates collection item. But then I get an error

ERROR (payload): NotFound: The requested resource was not found.

I followed the official documentation and the examples and I'm not sure what I'm missing.

Weirdly enough, I don't get the error when I use where: { id: doc.id }, instead of id: doc.id,. Then the user is created, their profile in Organizations/Candidates is also created, BUT the relationship is not updated.

Thank you in advance for your help. 🙂

solar badgerBOT
#

Help is on the way! To mark it as solved, use the /solve command. In the meantime, here are some existing threads that may help you:

Documentation:

Community-Help:

unkempt fox
#

So I think what this basically comes to is that after creating, somehow I can't fetch the same entry (user). I tried with payload.update and payload.findByID, both return

NotFound: The requested resource was not found.

While somehow the console.log returns it.

I moved the update logic to another hook and still the same behavior.

import type { AfterChangeHook } from 'payload/dist/collections/config/types'

export const updateProfileAfterCreate: AfterChangeHook = async ({ doc, req, req: { payload, body = {}, res }, operation, }) => {
    if (operation === 'create' && doc.id) {

        // Shows doc.id
        console.log('doc', doc)
        
        // Can't find the user's profile by doc.id
        await payload.findByID({
            collection: 'site-users',
            id: doc.id,
        })
    }
    return doc
}