Open hdodov opened 1 week ago
This leads to another error when trying to do a payload.update()
:
CastError: Cast to embedded failed for value "{...}" (type Object) at path "blocks" because of "CastError"
at model.Query.exec (webpack-internal:///(rsc)/./node_modules/.pnpm/mongoose@6.12.3_@aws-sdk+client-sso-oidc@3.679.0_@aws-sdk+client-sts@3.679.0_/node_modules/mongoose/lib/query.js:4921:21)
at Query.then (webpack-internal:///(rsc)/./node_modules/.pnpm/mongoose@6.12.3_@aws-sdk+client-sso-oidc@3.679.0_@aws-sdk+client-sts@3.679.0_/node_modules/mongoose/lib/query.js:5020:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
In the error message:
{...}
is the entire content of the document — I've removed it for brevity"blocks"
is the name of a field of type blocks
in the documentI think this cast error refers to the fact that Payload attempts to slide in the object { "en": "..." }
into a field that expects a string:
localize: true
, it's an object with each locale as the keylocalize: true
is removed, it is now expected to be a string
Describe the Bug
When you remove a field that has previously had
localized: true
and a value in the database, the front-end breaks.Link to the code that reproduces this issue
https://github.com/hdodov/test-payload/tree/localize-issues
Reproduction Steps
git clone git@github.com:hdodov/test-payload.git
pnpm docker
to start the projecthttp://localhost:3000/admin/collections/pages
slug
:foo
text
:hello
LOCALIZED=true
in.env
text
field tohello2
LOCALIZED=true
from.env
[object Object]
(not expected)Proposed Solution
Payload should check if fields are coming in the form of an object from the database. It already normalizes
{ en: "content" }
to"content"
when the field is marked as localized. It just has to do this when the field is not marked as localized too. In other words, it should function like this:"foo"
en
"foo"
"foo"
bg
"foo"
{ "en": "foo" }
en
"foo"
{ "en": "foo" }
bg
"foo"
"foo"
en
"foo"
"foo"
bg
"foo"
{ "en": "foo" }
en
"foo"
{ "en": "foo" }
bg
"foo"
{ "bg": "фуу" }
bg
""
…but the current behavior is this:
"foo"
en
"foo"
"foo"
bg
"foo"
{ "en": "foo" }
en
"foo"
{ "en": "foo" }
bg
"foo"
"foo"
en
"foo"
"foo"
bg
"foo"
{ "en": "foo" }
en
{ "en": "foo" }
{ "en": "foo" }
bg
{ "en": "foo" }
{ "bg": "фуу" }
bg
{ "bg": "фуу" }
Which area(s) are affected? (Select all that apply)
area: core
Environment Info