Prisma postgres DB not updating

Hello guys. I'm working on auth for my nextjs app built with nextauth and prisma. Initially, I created the project using a local sqlite db and everything worked properly. But when I switched to a postgres db with railway.app it stopped working properly. When I make a post request to create a new user account from Insomnia(an API Client), the db updates with the new user. But when I make that same request through the sign up form UI, the db doesn't update even after the fetch request returns an ok response. This is my signup function:
const onSubmit = async (data: FormData) => {
try {
const apiUrl = process.env.NEXTAUTH_URL;
const response = await fetch(`${apiUrl}/api/user`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.email,
password: data.password,
}),
});

if (response.ok) {
console.log("User created successfully!");
setConfirm(true);
// Handle success or redirect to another page if needed
} else {
console.error("Failed to create user.");
// Handle errors, show error messages, etc.
}
} catch (error) {
console.error("Error occurred:", error);
// Handle network errors or other exceptions
}
};
const onSubmit = async (data: FormData) => {
try {
const apiUrl = process.env.NEXTAUTH_URL;
const response = await fetch(`${apiUrl}/api/user`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: data.email,
password: data.password,
}),
});

if (response.ok) {
console.log("User created successfully!");
setConfirm(true);
// Handle success or redirect to another page if needed
} else {
console.error("Failed to create user.");
// Handle errors, show error messages, etc.
}
} catch (error) {
console.error("Error occurred:", error);
// Handle network errors or other exceptions
}
};
This is my api/user/route.ts file:
import prisma from "@/lib/prisma";
import * as bcrypt from "bcrypt";
import { NextRequest } from "next/server";

interface RequestBody {
email: string;
password: string;
}
export async function POST(request: Request | NextRequest) {
const body: RequestBody = await request?.json();

const user = await prisma.user.create({
data: {
email: body.email,
password: await bcrypt.hash(body.password, 10),
},
});

const { password, ...result } = user;
return new Response(JSON.stringify(result));
}
import prisma from "@/lib/prisma";
import * as bcrypt from "bcrypt";
import { NextRequest } from "next/server";

interface RequestBody {
email: string;
password: string;
}
export async function POST(request: Request | NextRequest) {
const body: RequestBody = await request?.json();

const user = await prisma.user.create({
data: {
email: body.email,
password: await bcrypt.hash(body.password, 10),
},
});

const { password, ...result } = user;
return new Response(JSON.stringify(result));
}
2 Replies
Alan Ibarra-2310
Alan Ibarra-231011mo ago
It might be process.env. It is only available on the server, so you could could console.log and see what it outputs. If you want to use environment variables on the client you have to prefix with NEXT_PUBLIC. https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#bundling-environment-variables-for-the-browser
Configuring: Environment Variables
Learn to add and access environment variables in your Next.js application.
victorwill
victorwill10mo ago
Thank you!