Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

paylaod error on array field update

default discord avatar
aragongot2 years ago
32

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
    }
  }
]
  • default discord avatar
    notchr.is2 years ago
    @451405703836336139

    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

  • default discord avatar
    aragongot2 years ago
  • default discord avatar
    notchr.is2 years ago

    Thank you, and the collection config if possible too

  • default discord avatar
    aragongot2 years ago
    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;
  • default discord avatar
    notchr.is2 years ago

    Out of curiosity, what if you omit the createAt and updatedAt properties from the offer.votes object



    can you spread those off

  • default discord avatar
    aragongot2 years ago

    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;
              }),
            ],
          },
        });
  • default discord avatar
    notchr.is2 years ago

    oh interesting

  • default discord avatar
    aragongot2 years ago

    and also when i map the types are not right

  • default discord avatar
    notchr.is2 years ago

    Can you do a findAll and log the the types of each likedBy



    hmm

  • default discord avatar
    aragongot2 years ago
  • default discord avatar
    notchr.is2 years ago

    wait



    likedBy is a relationship

  • default discord avatar
    aragongot2 years ago

    yes

  • default discord avatar
    notchr.is2 years ago

    I think you need to update it differently



    possibly

  • default discord avatar
    aragongot2 years ago

    ammm





    there is not createAt and updateAt



    so i think its omited

  • default discord avatar
    notchr.is2 years ago

    ahhh



    hmm

  • default discord avatar
    aragongot2 years ago

    its wired that it works here lol

  • default discord avatar
    notchr.is2 years ago

    can you regenerate your types?

  • default discord avatar
    aragongot2 years ago

    i will do it again sure

  • default discord avatar
    notchr.is2 years ago

    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

  • default discord avatar
    aragongot2 years ago

    yes it update like i want

  • default discord avatar
    notchr.is2 years ago

    interesting

  • default discord avatar
    aragongot2 years ago

    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

  • default discord avatar
    notchr.is2 years ago

    true



    why do the types of likedBy vary?

  • default discord avatar
    aragongot2 years ago

    likedBy?: (number | null) | User;

  • default discord avatar
    notchr.is2 years ago
  • default discord avatar
    aragongot2 years ago

    ok maybe i need to push userid instand of user object ?

  • default discord avatar
    notchr.is2 years ago

    I think that might be it, let me see if i can find an update relation example

  • default discord avatar
    aragongot2 years ago

    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;


    }),


    ],


    },


    });

  • default discord avatar
    notchr.is2 years ago

    WOOOO



    Very nice



    😄



    Nicely done

  • default discord avatar
    aragongot2 years ago

    thank you 🙏

  • default discord avatar
    notchr.is2 years ago

    Anytime!



    Cool if i mark this as solved?

  • default discord avatar
    aragongot2 years ago

    sure !

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.