Adding items to a relation on collection/doc update

default discord avatar
notchr
3 months ago
17

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 🙂 @jacobsfletch @alessiogr any ideas what I'm doing wrong here?

  • default discord avatar
    jarrod69420
    3 months 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
    Payload Team
    3 months ago

    that would be my suspicion too. depth too high

  • default discord avatar
    notchr
    3 months ago

    @jarrod69420 cool one sec!



    @jarrod69420 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?

  • default discord avatar
    jarrod69420
    3 months ago

    yeah probably 👍

  • default discord avatar
    notchr
    3 months ago

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



    I'll try this out now

  • default discord avatar
    jarrod69420
    3 months ago

    poor paul



    jkjk

  • default discord avatar
    notchr
    3 months ago

    @paulpopus 😉



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



    This issue is now resolvedd

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.