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?
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,
});
that would be my suspicion too. depth too high
@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?
yeah probably 👍
Thanks to you both, and paul too who I've been bugging behind the scenes hehe
I'll try this out now
poor paul
jkjk
@paulpopus 😉
Thanks all! The depth setting totally worked here!!!
This issue is now resolvedd
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.