Trouble w connecting prisma to next js
Answered
Rhinelander posted this in #help-forum
RhinelanderOP
1. installed prisma and created model
2. i made singleton:
3. I created route /api/test/
route.js:
What did I do wrong?
2. i made singleton:
import { PrismaClient } from "@prisma/client";
const client = globalThis.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalThis.prisma = client;
}
export default client;3. I created route /api/test/
route.js:
import prisma from "@/app/libs/prismadb"
export async function POST(){
return prisma.user.create({
data: {
name: "Jhon",
email: "Jhon@mail.com"
}
})
}What did I do wrong?
Answered by joulev
yes so you cannot write to the db because this constraint is violated. there is already an user with that email in the db and you had
email @unique53 Replies
@Rhinelander 1. installed prisma and created model
2. i made singleton:
`import { PrismaClient } from "@prisma/client";
const client = globalThis.prisma || new PrismaClient();
if (process.env.NODE_ENV !== 'production') {
globalThis.prisma = client;
}
export default client;`
3. I created route /api/test/
route.js:
`import prisma from "@/app/libs/prismadb"
export async function POST(){
return prisma.user.create({
data: {
name: "Jhon",
email: "Jhon@mail.com"
}
})
}`
What did I do wrong?
export async function POST(){
const result = await prisma.user.create({
data: {
name: "Jhon",
email: "Jhon@mail.com"
}
});
return NextResponse.json(result);
}RhinelanderOP
does not work
define 'does not work', what exactly is not working?
RhinelanderOP
it doesnt add it to db and it throws me server error with postman
what is the error message in the terminal
RhinelanderOP
it compiles successfully
no that is not the error message
RhinelanderOP
in postman or vs code
ok... in both
anywhere there is an error message
RhinelanderOP
405 method not allowed and UND_ERR_REQ_CONTENT_LENGTH_MISMATCH
cause: RequestContentLengthMismatchError: Request body length does not match content-length header
at write (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:67105)
at _resume (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:66726)
at resume (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:65413)
at connect (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:65301) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
at write (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:67105)
at _resume (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:66726)
at resume (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:65413)
at connect (C:\Users\Arnež\Desktop\test\node_modules\next\dist\compiled\undici\index.js:1:65301) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
@Rhinelander 405 method not allowed and UND_ERR_REQ_CONTENT_LENGTH_MISMATCH
method not allowed is due to you didn't submitting a POST request most likely
UND_ERR_REQ_CONTENT_LENGTH_MISMATCH is likely an old nextjs bug, upgrade nextjs to latest version
RhinelanderOP
i created project today
so its latest
i did npx i prisma -D then npx prisma init
after that i created libs/prismadb.js
created api/test/route.js and did POST function
@Rhinelander Click to see attachment
you never imported
prisma and NextResponseimport em
RhinelanderOP
at prismadb.js?
RhinelanderOP
now show the terminal
RhinelanderOP
before that it was
@Rhinelander before that it was
yes so you cannot write to the db because this constraint is violated. there is already an user with that email in the db and you had
email @uniqueAnswer
RhinelanderOP
db does not contain this
dunno – the error message literally says "unique constraint failed on the constraint:
User_email_key"the constraint is that
email @uniqueRhinelanderOP
yeah i just did not refresh my db and did not see it i am so dumb....
another question how could i display that data in frontend
if you don't have time to explain i would appreciate if you point me out to good resouce
export default async function Page() {
const users = await prisma.user.findMany();
return <pre>{JSON.stringify(users, null, 2)}</pre>;
}for a start
RhinelanderOP
I am gifted programer i get error for every single thing possible
so what are those 2 values after users
do they represent name and email
@Rhinelander Click to see attachment
the error message means there is a user with email: null in your db
change that
RhinelanderOP
sorry for being anoying i went from casual frontend javascript dev to fullstack nextjs dev in a week i lack a lot of knowledge but project that i need to do doesnt leave me with many options
yeah no problems i'm trying to help you here, but have you checked if there is a user without an email in your db?
because fairly sure there is
RhinelanderOP
@Rhinelander Click to see attachment
see the first user? that is
mail not emailRhinelanderOP
yeah i saw that and deleted it
it works
you are the best