medusajs/medusa · error · MedusaError

User with id "${userId}" not found

Error message

User with id "${userId}" not found

What it means

Thrown by POST /admin/users/:id/roles when the workflow target user does not exist. The route first queries the user entity by id and throws a NOT_FOUND MedusaError before invoking assignUserRolesWorkflow.

Source

Thrown at packages/medusa/src/api/admin/users/[id]/roles/route.ts:66

  req: AuthenticatedMedusaRequest<
    HttpTypes.AdminAssignUserRoles
  >,
  res: MedusaResponse
) => {
  const userId = req.params.id
  const { roles } = req.validatedBody
  const query = req.scope.resolve(ContainerRegistrationKeys.QUERY)

  const {
    data: [user],
  } = await query.graph({
    entity: "user",
    fields: ["id"],
    filters: { id: userId },
  })

  if (!user) {
    throw new MedusaError(
      MedusaError.Types.NOT_FOUND,
      `User with id "${userId}" not found`
    )
  }

  await assignUserRolesWorkflow(req.scope).run({
    input: {
      actor_id: req.auth_context.actor_id,
      actor: req.auth_context.actor_type,
      user_id: userId,
      role_ids: roles,
    },
  })

  const { data: links } = await query.graph({
    entity: "user_rbac_role",
    fields: ["rbac_role.*"],
    filters: { user_id: userId },

View on GitHub (pinned to 5e06e544a2)

Solutions

  1. Verify the user exists: GET /admin/users/{id}
  2. If soft-deleted, restore the user before assigning roles
  3. Copy the exact id from the users list endpoint
Defensive patterns

Strategy: validation

Validate before calling

const { users } = await medusa.admin.users.list({ q: userId })
if (!users.some(u => u.id === userId)) return skip()

Type guard

const isExistingUser = (u: {id: string} | undefined): u is {id: string} => !!u

Try / catch

try { await assignUserRoles(...) } catch (e) { if (e.type === 'not_found') refreshUserList(); else throw e }

Prevention

When it happens

Trigger: POST /admin/users/{id}/roles with a user id that was deleted, never existed, or contains a typo; also when soft-deleted users are filtered out by the remote query.

Common situations: Stale user id cached in the admin UI, deleting a user in another tab then assigning roles, or copying ids between environments (staging vs production).

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of medusajs/medusa@5e06e544a2 (2026-08-27). Data as JSON: /api/errors/0d9591453098aa80. Report an issue: GitHub.