paylaod throws this error when trying to update the entry with its data !
for simplicity i removed the map
ValidationError: The following field is invalid: votes.0.likedBy const offer = await payload.findByID({
collection: "offers",
depth: 4,
id: offerId,
disableErrors: true,
});
console.log("filed", [...offer.votes]);
await payload.update({
collection: "offers",
id: offerId,
data: {
votes: [...offer.votes],
},
});console log output
filed [
{
id: '65fc614539fe8e2bf466ece1',
type: '0',
likedBy: {
id: 1,
country_code: 'SA',
phone: 555555555,
balance: 0,
updatedAt: '2024-03-19T15:54:20.700Z',
createdAt: '2024-03-19T15:54:20.700Z',
email: 'admin@admin.com',
password: undefined,
loginAttempts: 0
}
}
]Can you check the stucture of a manually created offer?
Or share it here
I'm guessing its a mismatch, but it could be something else
If you have one made already, you can grab it from the API tab
Thank you, and the collection config if possible too
import { CollectionConfig } from "payload/types";
import { vote } from "../users/endpoints/votes";
const Offers: CollectionConfig = {
slug: "offers",
admin: {
useAsTitle: "coupon_code",
},
fields: [
{
name: "coupon_code",
label: "coupon code",
type: "text",
required: true,
},
{
name: "last_use",
label: "last use",
type: "date",
},
{
name: "description",
type: "richText",
},
{
name: "genres",
type: "relationship",
relationTo: "genres",
hasMany: true,
admin: {
position: "sidebar",
},
},
{
name: "partner",
type: "relationship",
relationTo: "partners",
admin: {
position: "sidebar",
},
},
{
name: "votes",
type: "array",
fields: [
{
name: "likedBy",
type: "relationship",
relationTo: "users",
},
{
name: "type",
type: "select",
options: [
{ label: "like", value: "1" },
{ label: "dislike", value: "0" },
],
},
],
// admin: { hidden: true },
},
],
endpoints: [
{
path: "/votes",
handler: vote,
method: "post",
},
],
};
export default Offers;Out of curiosity, what if you omit the createAt and updatedAt properties from the offer.votes object
can you spread those off
this is working tho
await payload.update({
collection: "offers",
id: offerId,
data: {
votes: [
...offer.votes.filter((vote) => {
if (typeof vote.likedBy === "object") {
return vote.likedBy.id !== userId;
}
return vote;
}),
],
},
});oh interesting
and also when i map the types are not right
Can you do a findAll and log the the types of each likedBy
hmm
wait
likedBy is a relationship
yes
I think you need to update it differently
possibly
ammm
there is not createAt and updateAt
so i think its omited
ahhh
hmm
its wired that it works here lol
can you regenerate your types?
i will do it again sure
ty
Yeah it is weird it worked there
But does
...offer.votes.filter((vote) => {
if (typeof vote.likedBy === "object") {
return vote.likedBy.id !== userId;
}
return vote;
}),`
produce a result
and if so, what structure
yes it update like i want
interesting
this is pusing new entry to votes
it can't by cuz the db right ? cuz paylaod does not remove the rels from the db if you remove them from the admin panel
true
why do the types of likedBy vary?
likedBy?: (number | null) | User;
ok maybe i need to push userid instand of user object ?
I think that might be it, let me see if i can find an update relation example
that did work !
await payload.update({
collection: "offers",
id: offerId,
data: {
votes: [
...offer.votes.map((vote) => {
if (typeof vote.likedBy === "object") {
return { ...vote, likedBy: vote.likedBy.id };
}
return vote;
}),
],
},
});
WOOOO
Very nice
😄
Nicely done
thank you 🙏
Anytime!
Cool if i mark this as solved?
sure !
Star
Discord
online
Get dedicated engineering support directly from the Payload team.