You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: saveAchievements throws 500 error when unlockedAchievements is an empty array #2394
In backend/controllers/achievementController.js, saveAchievements validates that unlockedAchievements is an array:
if(!unlockedAchievements||!Array.isArray(unlockedAchievements)){returnres.status(400).json({success: false,error: "unlockedAchievements must be a valid array"});}
When unlockedAchievements is an empty array [] (e.g. { "unlockedAchievements": [] }), it passes the validation check and attempts to update MongoDB:
In MongoDB, the $each operator requires a non-empty array. Calling $each: [] causes MongoDB to reject the update with MongoServerError: The $each special array operator must be used with a non-empty array (code 2). This error is caught in the catch block and returns an unhandled HTTP 500 Internal Server Error ("A server error occurred").
Steps to Reproduce
Authenticate to the PrepPilot API.
Send a POST request to /api/achievements with payload { "unlockedAchievements": [] }.
Notice that the server throws MongoServerError and responds with HTTP 500 { "success": false, "error": "A server error occurred" }.
Expected Behavior
Passing an empty array [] should be handled gracefully without executing an invalid $each: [] MongoDB update, returning { "success": true }.
Actual Behavior
MongoDB rejects the $each: [] update query and the controller returns HTTP 500 Internal Server Error.
Severity
Medium
Additional Context
Checking if (unlockedAchievements.length === 0) can safely early return { success: true }, avoiding the illegal $each: [] MongoDB operation.
Fix implemented and submitted in PR #2395 (#2395). All unit tests in achievementController.unit.test.js pass cleanly (10/10), currently open awaiting maintainer review.
Activity
shrinivas-sn commentedon Oct 25, 2026
/claim
github-actions commentedon Oct 25, 2026
Thanks for creating this issue!
If you'd like to work on it, simply comment:
The first person to claim the issue will be assigned automatically.
Happy contributing!
github-actions commentedon Oct 25, 2026
✅ @shrinivas-sn has successfully claimed this issue!
You've been assigned. Happy coding!
Maanusri-2008 commentedon Oct 31, 2026
/claim
github-actions commentedon Oct 31, 2026
❌ This issue has already been claimed by @shrinivas-sn.
Please look for another open issue or create a new one to contribute.
Thank you for your interest!
shrinivas-sn commentedon Nov 11, 2026
Fix implemented and submitted in PR #2395 (#2395). All unit tests in achievementController.unit.test.js pass cleanly (10/10), currently open awaiting maintainer review.