Problem with Prisma
this is my controller
async function signUp(req: Request, res: Response) {
req.body.password = await hashPassword(req.body.password);
req.body.birthDate = new Date(req.body.birthDate).toISOString();
try {
delete req.body.repeat_password;
const user = await User.createUser(req.body);
res.status(200).json(user);
} catch (error) {
if (error instanceof PrismaClientKnownRequestError)
res.status(400).json({ code: error.code, message: error.message });
else if (error instanceof Error)
res.status(400).json({
message: error.message || "Error Signing up user",
});
else res.status(400).json(error);
}
}
and this is my model function
import { Prisma, PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function createUser(user: User) {
try {
const result = await prisma.user.create({
data: {
username: user.username,
birthDate: user.birthDate,
password: user.password,
},
});
return result;
} catch (error) {
if (error instanceof Prisma.PrismaClientKnownRequestError) throw error;
throw error;
}
}
when i request to sign up user it return this error
{
"message": "\nInvalid
prisma.user.create() invocation in\nC:\\Users\\ghera\\OneDrive\\Desktop\\GithubRepo\\Book_Tracker\\server\\src\\models\\userModel.ts:29:38\n\n 26 \n 27 async function createUser(user: User) {\n 28 try {\n→ 29 const result = await prisma.user.create(\nerror: Environment variable not found: DATABASE_URL."
}
i tried other functions and they work fine ( as expected ) retrieving books from data works fine etc...4 Replies
Environment variable not found: DATABASE_URL. Looks like you're missing an environment variable
other queries run just fine
they connect to the same database using the same database_url
even running npx prisma db pull and npx prisma migrate is fine
btw hardcoing it in this specific model seems to work
const prisma = new PrismaClient({
datasources: {
db: {
url: "prisma+postgres://accelerate.prisma-data.net/?api_key=SOMERANDOMLETTERsI",
},
},
});
what triggers me the most why only this specific model is throwing this error other models work finecouldn't you do
url: env(DATABASE_URL)
or something?i have a prisma.schema file
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
and three other models that connect normally they get the url from prisma.schema ( i followed the docs ) but the User Model cant find the URL for some reason and keep throwing that error