payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
25.73k stars 1.64k forks source link

"id" and "data" parameters are undefined in access read function. #8183

Closed poofy25 closed 2 months ago

poofy25 commented 2 months ago

Link to reproduction

No response

Environment Info

Binaries:
  Node: 20.14.0
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  payload: 3.0.0-beta.102
  next: 15.0.0-canary.104
  @payloadcms/db-mongodb: 3.0.0-beta.102
  @payloadcms/email-nodemailer: 3.0.0-beta.102
  @payloadcms/graphql: 3.0.0-beta.102
  @payloadcms/next/utilities: 3.0.0-beta.102
  @payloadcms/plugin-cloud: 3.0.0-beta.102
  @payloadcms/richtext-lexical: 3.0.0-beta.102
  @payloadcms/translations: 3.0.0-beta.102
  @payloadcms/ui/shared: 3.0.0-beta.102
  react: 19.0.0-rc-06d0b89e-20240801
  react-dom: 19.0.0-rc-06d0b89e-20240801
Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 11 Home
  Available memory (MB): 15616
  Available CPU cores: 16

Describe the Bug


export const Courses: CollectionConfig = {
  slug: 'courses',
  admin: {
    useAsTitle: 'name',
  },
  access: {
    read: ({ req, id, data }) => {
      const user = req.user
      console.log(id , data) // both are logged as undefined

      if (user?.collection === 'users' && user.courses) {
        if (user.courses && user.courses.includes(String(id))) return true
      }
      return false //this always returns false because the id is undefied
    },
  },

  fields: [
    {
      name: 'name',
      label: 'Nume',
      required: true,
      type: 'text',
    },
  ],
}

I have a Users collection that have a relationship to this Courses collection and I need to check if the user has the fetched course included in the relationship.

But in the access read function the id and data of the document always return undefined, I also saw some other people having the same issue on discord.

Reproduction Steps

Create a collection and log the id and data parameters to the console.


export const CollectionName: CollectionConfig = {
  slug: 'collectionSlug',
  admin: {
    useAsTitle: 'test',
  },
  access: {
    read: ({ id, data }) => {
      console.log(id , data) // both are logged as undefined
      return true
    },
  },

  fields: [
    {
      name: 'test',
      label: 'test',
      required: true,
      type: 'text',
    },
  ],
}

Adapters and Plugins

No response

hristokoev commented 2 months ago

As per the docs, only the following arguments are provided to the read function: req and id, but data appears to be also there.

On the same beta as yours, this:

access: {
  read: ({ id, data }) => {
    console.log(id, data)
    return true
  }
}

Results in undefined undefined when I'm in the collections view and then it's not undefined (it logs the id and data) when I open a single document. This is expected behaviour. Are you getting undefined when you open the document as well?

Have you tried using a Where query instead of returning a boolean? Something like:

read: ({ req: { user } }) => {
  return {
    id: {
      in: user?.courses,
    },
  }
}

Then in your Users collection, the relationship field courses needs to have hasMany: true and maxDepth: 0 as to make the user.courses an array of IDs.

github-actions[bot] commented 2 months ago

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.