Skip to content

[Bug]: saveAchievements throws 500 error when unlockedAchievements is an empty array #2394

Open

Description

@shrinivas-sn

Bug Description

In backend/controllers/achievementController.js, saveAchievements validates that unlockedAchievements is an array:

if (!unlockedAchievements || !Array.isArray(unlockedAchievements)) {
    return res.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:

await User.findByIdAndUpdate(
    req.user._id,
    { $addToSet: { unlockedAchievements: { $each: unlockedAchievements } } },
);

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

  1. Authenticate to the PrepPilot API.
  2. Send a POST request to /api/achievements with payload { "unlockedAchievements": [] }.
  3. 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.

Activity

  1. shrinivas-sn commented on Oct 25, 2026

    @shrinivas-sn
    Author

    /claim

  2. github-actions commented on Oct 25, 2026

    @github-actions

    Thanks for creating this issue!

    If you'd like to work on it, simply comment:

    /claim

    The first person to claim the issue will be assigned automatically.

    Happy contributing!

  3. github-actions commented on Oct 25, 2026

    @github-actions

    ✅ @shrinivas-sn has successfully claimed this issue!

    You've been assigned. Happy coding!

    Tip: Please open a Pull Request linked to this issue when you're ready.
    Add Closes #2394 in your PR description.

  4. Maanusri-2008 commented on Oct 31, 2026

    @Maanusri-2008

    /claim

  5. github-actions commented on Oct 31, 2026

    @github-actions

    ❌ 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!

  6. shrinivas-sn commented on Nov 11, 2026

    @shrinivas-sn
    Author

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

claimedThis issue has been claimed by a contributor

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions