"Database operation failed" error with Vercel KV in Next.js app

Hello Vercel Community,

I’m encountering a persistent issue with my Next.js application using Vercel KV for user registration. Despite following the documentation and implementing several debugging steps, I’m still unable to resolve this problem.

Issue:
When attempting to register a new user, I receive a “Database operation failed” error. The client-side error message is:

Steps taken:

  1. Verified all required environment variables are set (KV_URL, KV_REST_API, KV_REST_API_TOKEN, KV_REST_API_READ_ONLY_TOKEN).
  2. Implemented detailed server-side logging in the registration API route.
  3. Created a debug endpoint to test Vercel KV connection.
  4. Checked Vercel logs for any error messages during registration attempts.

Relevant code:

Registration API route (app/api/auth/register/route.ts):

import { NextResponse } from "next/server"
import { hash } from "bcryptjs"
import { kv } from "@vercel/kv"

export async function POST(request: Request) {
  console.log("Registration route called")
  try {
    const body = await request.json()
    console.log("Request body:", JSON.stringify(body))

    const { name, company, email, password } = body

    if (!name || !company || !email || !password) {
      console.log("Missing required fields")
      return NextResponse.json({ message: "All fields are required" }, { status: 400 })
    }

    try {
      console.log("Checking if user already exists")
      const existingUser = await kv.get(`user:${email}`)
      console.log("Existing user check result:", existingUser)

      if (existingUser) {
        console.log("Email already exists")
        return NextResponse.json({ message: "Email already registered" }, { status: 400 })
      }

      console.log("Hashing password")
      const hashedPassword = await hash(password, 10)

      const newUser = {
        name,
        company,
        email,
        password: hashedPassword,
      }

      console.log("Attempting to save new user")
      await kv.set(`user:${email}`, JSON.stringify(newUser))

      console.log("User registered successfully:", JSON.stringify(newUser))
      return NextResponse.json({ message: "User registered successfully" }, { status: 201 })
    } catch (dbError) {
      console.error("Database operation error:", dbError)
      return NextResponse.json({ 
        message: "Database operation failed", 
        error: dbError.toString(),
        stack: dbError.stack
      }, { status: 500 })
    }
  } catch (error) {
    console.error("Unhandled registration error:", error)
    return NextResponse.json({ 
      message: "Internal server error", 
      error: error.toString(),
      stack: error.stack
    }, { status: 500 })
  }
}

Hi, Nikhil! Welcome to the Vercel Community :wave:

Could you share a minimal reproducible example with us?

I’d also like to share some marketplace options, in case it’s helpful → Vercel Marketplace

One thing that may be causing this is Vercel KV has been sunset, and existing stores were automatically migrated to Upstash Redis in December 2024. If you’re still using the @vercel/kv package, it may not be fully compatible with the migrated backend.

I’d recommend:

  • Switching from @vercel/kv to @upstash/redis (docs here)
  • Double-checking your environment variables in the Vercel dashboard — the connection strings may have changed after the migration
  • If the store isn’t working at all, you can create a fresh one through the Vercel Marketplace

Let me know if that helps!