some experiments with the new join field in PayloadCMS
PayloadCMS versions: v3.0.0-beta.(108 - 114)
Note
Seems to be fixed in v3.0.0-beta.115
Currently, if you have an auth enabled collection (e.g. users
) with an array field and a join field
and the array field has a AfterChange field hook, then the previousValue
argument in this hook will sometimes be overridden with the join value.
So this collection:
import {CollectionConfig} from 'payload'
export const Users: CollectionConfig = {
slug: 'users',
labels: {
singular: 'User',
plural: 'Users'
},
auth: true,
fields: [
{
name: 'someArray',
type: 'array',
hooks: {
afterChange: [({previousValue, value}) => {
console.log('someArrayAfterChangeFieldHookPrevValue: ', previousValue)
console.log('someArrayAfterChangeFieldHookValue: ', value)
}]
},
fields: [
{
name: 'someField',
type: 'text'
}
]
},
{
name: 'projects',
type: 'join',
collection: 'projects',
on: 'user'
}
]
}
will log
someArrayAfterChangeFieldHookPrevValue: {
projects: { docs: [ '66f039149f5df29751d2ab3d' ], hasNextPage: false }
}
someArrayAfterChangeFieldHookValue: [ { someField: 'uiae', id: '66f039188ab03b0022db5051' } ]
But someArrayAfterChangeFieldHookPrevValue
should NEVER be an object. It is always an array or undefined.
This bug makes using the join field in this matter impossible.
- have
docker
andmake
installed on your system git clone
this repository- run
make start
- navigate to
http://localhost
- log in with the default account (already prefilled)
- go to collection
Projects
and create a Project (e.g. name it 'Test') with the default user selected in theuser
field - go to collection
Users
and edit the default user. - Now if you add some entry to the array field
someArray
, save the changes and reload the document, the changes are gone!!! Also the console will log the above error.