From d69f7c1f98d45fe6a49869d312901c899b37b1c3 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Tue, 7 Oct 2025 21:35:55 +0200 Subject: [PATCH] Fix ObjectId casting error when jobs relationship is populated MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the email's jobs relationship is populated with full job objects instead of just IDs, calling String(job) on an object results in "[object Object]", which causes a Mongoose ObjectId casting error. This fix properly extracts the ID from job objects or uses the value directly if it's already an ID. Fixes job scheduler error: "Cast to ObjectId failed for value '[object Object]'" 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- package.json | 2 +- src/utils/jobScheduler.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 7409fb6..1ff2df2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@xtr-dev/payload-mailing", - "version": "0.4.18", + "version": "0.4.19", "description": "Template-based email system with scheduling and job processing for PayloadCMS", "type": "module", "main": "dist/index.js", diff --git a/src/utils/jobScheduler.ts b/src/utils/jobScheduler.ts index 6d09751..cbfed6c 100644 --- a/src/utils/jobScheduler.ts +++ b/src/utils/jobScheduler.ts @@ -129,7 +129,11 @@ export async function updateEmailJobRelationship( id: normalizedEmailId, }) - const currentJobs = (currentEmail.jobs || []).map((job: any) => String(job)) + // Extract IDs from job objects or use the value directly if it's already an ID + // Jobs can be populated (objects with id field) or just IDs (strings/numbers) + const currentJobs = (currentEmail.jobs || []).map((job: any) => + typeof job === 'object' && job !== null && job.id ? String(job.id) : String(job) + ) const allJobs = [...new Set([...currentJobs, ...normalizedJobIds])] // Deduplicate with normalized strings await payload.update({