diff --git a/packages/backend/src/routes/songs.ts b/packages/backend/src/routes/songs.ts index 6b5b10a..8be0122 100644 --- a/packages/backend/src/routes/songs.ts +++ b/packages/backend/src/routes/songs.ts @@ -172,13 +172,17 @@ router.get('/playlist/*', async (req: Request, res: Response) => { return total + seconds; }, 0); - // Get songs with pagination - const songs = await Song.find(query) - .sort({ title: 1 }) - .skip(skip) - .limit(limit) + // Get songs for this page in the exact playlist order + const pageStart = (page - 1) * limit; + const pageEnd = Math.min(pageStart + limit, trackIds.length); + const pageTrackIds = trackIds.slice(pageStart, pageEnd); + + const pageSongs = await Song.find({ id: { $in: pageTrackIds } }) .populate('s3File.musicFileId') .lean(); + const idToSong: Record = {}; + for (const s of pageSongs) idToSong[s.id] = s; + const songs = pageTrackIds.map(id => idToSong[id]).filter(Boolean); console.log(`Found ${songs.length} songs for playlist "${playlistName}" (${totalSongs} total), ${songs.filter((s: any) => s.s3File?.hasS3File).length} with S3 files`);