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.

Adding items to a relation on collection/doc update

default discord avatar
notchr2 years ago
7

Hello all! I'm currently making a custom endpoint on a collection where I want to add a document to a relation.



      handler: async (req, res, next) => {
        const { applicantId, positionId } = req.body;
        console.log(applicantId, positionId);

        try {
          const position = await req.payload.findByID({
            collection: "job-positions",
            id: positionId,
          });

          let updateApplicants = [...position.applicants, applicantId];

          const doUpdate = await req.payload.update({
            collection: "job-positions",
            id: positionId,
            data: {
              applicants: updateApplicants,
            },
          });

          res.status(200).json({
            ok: true,
          });
        } catch (error) {
          res.status(400).json({
            ok: false,
            error: "Error while applying for position.",
          });
        }
      },


Job Positions has an 'applicants' field which should store the id's of Applicants that apply. When updating the position, I get an error

BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer

What is the correct way to do this?



Thanks!!



I can see in the JSON that the relation field gets populated with the whole record. Ideally it would only store the ID, right?



Bump 🙂

@808734492645785600

@360823574644129795

any ideas what I'm doing wrong here?

  • discord user avatar
    jarrod_not_jared
    2 years ago

    Are you looking at the DB?



    It stores the ID, so if you query with depth set high or low enough you will either get back just the ID or the full doc



    1m let me read through your example again



    change your find to this:


    const position = await req.payload.findByID({
      collection: "job-positions",
      id: positionId,
      depth: 0,
    });
  • discord user avatar
    alessiogr
    2 years ago

    that would be my suspicion too. depth too high

  • default discord avatar
    notchr2 years ago
    @281120856527077378

    cool one sec!



    @281120856527077378

    I'm also having a similar issue, but this time with an upload document, it's somewhat related, should I make a new post for that?

  • discord user avatar
    jarrod_not_jared
    2 years ago

    yeah probably 👍

  • default discord avatar
    notchr2 years ago

    Thanks to you both, and paul too who I've been bugging behind the scenes hehe



    I'll try this out now

  • discord user avatar
    jarrod_not_jared
    2 years ago

    poor paul



    jkjk

  • default discord avatar
    notchr2 years ago
    @858693520012476436

    😉



    Thanks all! The depth setting totally worked here!!!



    This issue is now resolvedd

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.