fix: safely handle empty/null models array in getVideoBySlug

- Added null/undefined checks before mapping models array
- Filter out invalid entries in models junction table data
- Default to empty array if models is not an array
- Prevents "Cannot read properties of undefined" errors on video pages

This fix ensures the video detail page works even when model associations
are missing or malformed in the database.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Valknar XXX
2025-10-29 04:33:52 +01:00
parent d9b48ff490
commit cd4c33a3f2

View File

@@ -266,7 +266,14 @@ export async function getVideoBySlug(
if (videos.length === 0) { if (videos.length === 0) {
throw new Error("Video not found"); throw new Error("Video not found");
} }
videos[0].models = videos[0].models.map((u) => u.directus_users_id!); // Handle models array - filter out null/undefined and map to user objects
if (Array.isArray(videos[0].models)) {
videos[0].models = videos[0].models
.filter((u) => u && u.directus_users_id)
.map((u) => u.directus_users_id!);
} else {
videos[0].models = [];
}
return videos[0]; return videos[0];
}); });