overleaf/overleaf · error · NotFoundError

no '${req.entityName}' entity with '${req.params.id}'

Error message

no '${req.entityName}' entity with '${req.params.id}'

What it means

This NotFoundError is thrown by the requireEntity middleware in UserMembershipMiddleware.mjs when a request references an entity name plus an ID, but no entity matching req.params.id has been loaded onto req. The middleware guards routes that expect req.entity to be populated by an earlier middleware; if it is still missing, it responds with a 404 instead of letting the handler run on undefined data.

Source

Thrown at services/web/app/src/Features/UserMembership/UserMembershipMiddleware.mjs:379

      return fetchEntity()(req, res, next)
    } else {
      return next()
    }
  }
}

// ensure an entity was found, or fail with 404
function requireEntity() {
  return (
    /** @type {any} */ req,
    /** @type {any} */ res,
    /** @type {any} */ next
  ) => {
    if (req.entity) {
      return next()
    }

    throw new Errors.NotFoundError(
      `no '${req.entityName}' entity with '${req.params.id}'`
    )
  }
}

/**
 * ensure an entity was found or redirect to entity creation page if the user
 * has permissions to create the entity, or fail with 404
 */
function requireEntityOrCreate() {
  return (
    /** @type {any} */ req,
    /** @type {any} */ res,
    /** @type {any} */ next
  ) => {
    if (req.entity) {
      return next()
    }

View on GitHub (pinned to 28ad3b03b7)

Solutions

  1. Verify the entity ID in the request URL exists and has not been deleted
  2. Ensure the middleware that loads req.entity (e.g. a fetch-entity middleware) is registered before requireEntity in the route chain
  3. Check that the entityName route parameter matches the actual entity collection being queried
  4. Return/inspect the 404 response rather than retrying — it is an intentional not-found signal

Example fix

// before
app.get('/entities/:entityName/:id', requireEntity, handler) // req.entity never loaded
// after
app.get('/entities/:entityName/:id', loadEntity, requireEntity, handler) // loadEntity sets req.entity
Defensive patterns

Strategy: try-catch

Validate before calling

const entityId = req.params.id
if (!req.entity && !entityId) throw new ClientError('entity id required')

Try / catch

try {
  await fetch(url)
} catch (e) {
  if (e?.info?.statusCode === 404) return showEntityNotFound()
  throw e
}

Prevention

When it happens

Trigger: A route handler registered after requireEntity runs, but the middleware responsible for loading the entity (by req.params.id) never attached req.entity — typically because the ID does not correspond to any existing entity, or the loading middleware was not chained before this one.

Common situations: Users navigating to stale or deleted entity URLs; clients sending fabricated or mistyped entity IDs; developers mounting requireEntity without the entity-loading middleware upstream; entity was deleted between listing and fetch.

Related errors


AI-assisted analysis of overleaf/overleaf@28ad3b03b7 (2026-09-03). Data as JSON: /api/errors/b41d877ba66f6f54. Report an issue: GitHub.