Drizzle NextJs Middleware

Hello, I'm currently using NextJs middlewares to protect my application. In my middleware, I have a query running to check if the sessionToken is valid. But whenever I visit the protected page I get the following error.
Server Error
TypeError: edgeFunction is not a function

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
runWithTaggedErrors
file:///E:/APPLICATION/node_modules/next/dist/server/web/sandbox/sandbox.js (96:30)
async DevServer.runMiddleware
file:///E:/APPLICATION/node_modules/next/dist/server/next-server.js (962:24)
async DevServer.runMiddleware
file:///E:/APPLICATION/node_modules/next/dist/server/dev/next-dev-server.js (250:28)
async DevServer.handleCatchallMiddlewareRequest
file:///E:/APPLICATION/node_modules/next/dist/server/next-server.js (1051:22)
async DevServer.handleRequestImpl
file:///E:/APPLICATION/node_modules/next/dist/server/base-server.js (736:32)
Server Error
TypeError: edgeFunction is not a function

This error happened while generating the page. Any console logs will be displayed in the terminal window.
Call Stack
runWithTaggedErrors
file:///E:/APPLICATION/node_modules/next/dist/server/web/sandbox/sandbox.js (96:30)
async DevServer.runMiddleware
file:///E:/APPLICATION/node_modules/next/dist/server/next-server.js (962:24)
async DevServer.runMiddleware
file:///E:/APPLICATION/node_modules/next/dist/server/dev/next-dev-server.js (250:28)
async DevServer.handleCatchallMiddlewareRequest
file:///E:/APPLICATION/node_modules/next/dist/server/next-server.js (1051:22)
async DevServer.handleRequestImpl
file:///E:/APPLICATION/node_modules/next/dist/server/base-server.js (736:32)
Does anyone know how I can fix this? If needed, I can provide some code examples.
3 Replies
Angelelz
Angelelz14mo ago
Nextjs middleware run in a different environment, edge compatible, not node. Your code is probably calling some node functionality. This is not related to Drizzle
Cyan
CyanOP14mo ago
This is what I have in my middleware.
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { db } from "./db/db";
import { sessions } from "./db/schema";
import { eq } from "drizzle-orm";

export async function middleware(request: NextRequest) {
const queryData = await db.query.sessions.findFirst({
where: eq(sessions.sessionToken, request.cookies.get('next-auth.session-token')?.value as string),
})

return NextResponse.next()
}

export const config = {
matcher: '/placeholder',
}
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
import { db } from "./db/db";
import { sessions } from "./db/schema";
import { eq } from "drizzle-orm";

export async function middleware(request: NextRequest) {
const queryData = await db.query.sessions.findFirst({
where: eq(sessions.sessionToken, request.cookies.get('next-auth.session-token')?.value as string),
})

return NextResponse.next()
}

export const config = {
matcher: '/placeholder',
}
Angelelz
Angelelz14mo ago
The driver you're using might not be edge compatible then
Want results from more Discord servers?
Add your server