Skip to content

useEvent is not defined #756

@shaunnbarron

Description

@shaunnbarron

Describe the bug
After migrating my own Drizzle setup to Nuxt Hub, when using a function from server/utils inside of server/db/schema.ts, useEvent is not defined. This had previously worked with my own Drizzle setup.

Steps to reproduce
Steps to reproduce the behavior:

  1. Create an empty Nuxt project
  2. Enable Nuxt Hub and Nuxt Hub Database with Postgresql following the Nuxt Hub docs
  3. Add a util for audit fields:
// server/utils/audit.ts

export function getRequestingUserId(): string {
  const event = useEvent();
  return event.context.user.id ?? 'anonymous';
}
  1. Ensure asyncContext is enabled:
export default defineNuxtConfig({
  compatibilityDate: '2025-07-15',
  devtools: { enabled: true },
  modules: ['@nuxthub/core'],
  hub: {
    db: 'postgresql'
  },
  experimental: {
    asyncContext: true
  },
  nitro: {
    experimental: {
      asyncContext: true
    }
  }
})
  1. Add a schema using the audit fields:
// server/db/schema.ts

import { pgTable, text, serial, timestamp } from 'drizzle-orm/pg-core'
import { getRequestingUserId } from '../utils/audit'

const audit = {
  createdAt: timestamp().defaultNow(),
  updatedAt: timestamp().$onUpdate(() => new Date()),
  createdBy: text().notNull().$defaultFn(getRequestingUserId),
  updatedBy: text().$onUpdateFn(getRequestingUserId),
};

export const users = pgTable('users', {
  id: serial().primaryKey(),
  name: text().notNull(),
  email: text().notNull().unique(),
  password: text().notNull(),
  avatar: text().notNull(),
  ...audit,
})
  1. Try to write to the db:
// server/api/users.post.ts

export default defineEventHandler(async (event) => {
    await db.insert(schema.users).values({
        name: "John Doe",
        email: "john@example.com",
        password: "securepassword",
        avatar: "https://example.com/avatar.jpg"
    })
})

Expected behavior
The audit fields should be populated and written to the database. When running with my own Drizzle setup, this works. After migrating to Nuxt Hub I get the following:

ReferenceError {
  stack: 'useEvent is not defined\n' +
  'at PgText.getRequestingUserId [as defaultFn] (./.nuxt/hub/db/schema.mjs:5:2)\n' +
  'at PgDialect.buildInsertQuery (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/dialect.js:379:43)\n' +
  'at QueryPromise.getSQL (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/query-builders/insert.js:157:25)\n' +
  'at ./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/query-builders/insert.js:166:69)\n' +
  'at Object.startActiveSpan (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgre'... 1201 more characters,
  message: 'useEvent is not defined',
}

My custom Drizzle setup was not that different to Nuxt Hub. I had to make very minimal changes to migrate to Nuxt Hub.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions