payload
payload copied to clipboard
[V3] findByID error on media collection in beforeValidate hook
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
- Add an image to the
mediacollection, any jpg will do - Add an entry to the
democollection, and add 1 block that relates to the image - A
Something went wrongerror 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
The hook should have req, and you should use req.payload.findByID. Can you try that?
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?
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
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.
any update?
any update?
Just use payload on the req and do not import payload directly.
This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.