payload icon indicating copy to clipboard operation
payload copied to clipboard

[V3] findByID error on media collection in beforeValidate hook

Open JeffreyArts opened this issue 1 year ago • 4 comments

Describe the Bug

Not 100% certain if this is a bug, or if it is something that I missed (or is missing) in the migration guide. But after migrating from V2 to V3, I noticed that I no longer could save my pages. I have been able to isolate the bug and put that in a repo for you to reproduce. In short;

The following code

hooks: {
    beforeValidate: [
        async ({data}) => {
            const res = await payload.findByID({
                collection: "media",
                id: data.blocks[0].id,
            })
        }
    ]
},

Will throw the following error

ERROR: The collection with slug media can't be found. Find By ID Operation.

err: {
    "type": "APIError",
    "message": "The collection with slug media can't be found. Find By ID Operation.",
    "data": null,
    "isOperational": true,
    "isPublic": false,
    "status": 500,
    "name": "APIError"
}

Link to the code that reproduces this issue

https://github.com/JeffreyArts/payload3-blocks-findByID-error

Reproduction Steps

  1. Add an image to the media collection, any jpg will do
  2. Add an entry to the demo collection, and add 1 block that relates to the image
  3. A Something went wrong error will occur in the UI and the entry won't save ( The collection with slug media can't be found. Find By ID Operation will be thrown in the CLI)

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

Payload: 3.1.0
NodeJS: 20.10.0
NextJS: 15.0.0

JeffreyArts avatar Nov 24 '24 20:11 JeffreyArts

The hook should have req, and you should use req.payload.findByID. Can you try that?

JarrodMFlesch avatar Nov 24 '24 20:11 JarrodMFlesch

That resolves it.

Just for my understanding. Is that because in V2, payload was initiated as a global object? By which I mean that the payload object that you import from node_modules got configured/modified, but in V3 this is no longer the case. When I import payload in V3, it always retrieves the "clean", "untouched", "fresh" payload object?

JeffreyArts avatar Nov 25 '24 06:11 JeffreyArts

The hook should have req, and you should use req.payload.findByID. Can you try that?

FYI: Had to use req in a FieldLabelServerComponent's. Importing payload direct threw the same error as @JeffreyArts

const UseAsLabel: CheckboxFieldLabelServerComponent = async ({ req, siblingData }) => {
  const result = await req.payload.find({
    collection: 'posts
    where: {
      id: {
        equals: siblingData.post
      }
    }
  })
   
  return `Hey`
}

export default UseAsLabel

rolandsaven avatar Nov 26 '24 02:11 rolandsaven

This issue has been marked as stale due to lack of activity.

To keep this issue open, please indicate that it is still relevant in a comment below.

github-actions[bot] avatar Dec 19 '24 05:12 github-actions[bot]

any update?

ducmaster01 avatar Jul 01 '25 04:07 ducmaster01

any update?

Just use payload on the req and do not import payload directly.

JarrodMFlesch avatar Jul 15 '25 18:07 JarrodMFlesch

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

github-actions[bot] avatar Jul 23 '25 05:07 github-actions[bot]