我正在做一个学校项目,我正在使用NextJS 13,并试图使用Prisma与PlanetScale连接到MYSQL数据库,但在尝试注册用户时,我得到:
Request error PrismaClientValidationError:
Invalid `prisma.user.create()` invocation:
{
data: {
+ cpf: String,
+ name: String,
+ email: String,
+ password: String,
+ phone: String,
+ gender: String,
+ birth: DateTime,
+ city: String,
+ state: String,
+ school: String,
+ bio: String,
? avatar?: String | null
}
}
Argument cpf for data.cpf is missing.
Argument name for data.name is missing.
Argument email for data.email is missing.
Argument password for data.password is missing.
Argument phone for data.phone is missing.
Argument gender for data.gender is missing.
Argument birth for data.birth is missing.
Argument city for data.city is missing.
Argument state for data.state is missing.
Argument school for data.school is missing.
Argument bio for data.bio is missing.
src/app/API/user上的代码:
import prisma from "../../../lib/prisma";
import { NextResponse } from "next/server"
export async function POST(request) {
const body = request.body
try {
const newUser = await prisma.user.create({
data: {
cpf: body.cpf,
name: body.name,
email: body.email,
password: body.password,
phone: body.phone,
gender: body.gender,
birth: body.birth,
city: body.city,
state: body.state,
school: body.school,
bio: body.bio,
// avatar: body.avatar || null,
}
})
return NextResponse.json({ data: newUser, success: true });
} catch (error) {
console.error('Request error', error)
return NextResponse.json({ error: 'Error creating user', success: false }, { status: 500 });
}
}
schema.prisma:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}
model User {
id Int @id @default(autoincrement())
cpf String @unique
name String
email String @unique
password String
phone String
gender String
birth DateTime @db.Date
city String
state String
school String
bio String
avatar String?
}
src/app/login/page.tsx上的代码:
async function handleSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();
let body = { cpf, name, email, password, phone, gender, birth, city, state, school, bio};
try {
const response = await fetch('/api/user', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
if (response.status !== 200) {
throw new Error(await response.text())
} else {
console.log('Cadastro realizado com sucesso!');
}
} catch (error) {
console.log(body);
console.log(error);
}
}
如果我将API代码修改为:
const newUser = await prisma.user.create({
data: {
cpf: "12345678901",
name: "John Doe",
email: "johndoe@example.com",
password: "password123",
phone: "1234567890",
gender: "male",
birth: new Date(),
city: "New York",
state: "NY",
school: "Johns Hopkins University",
bio: "I am a software engineer and I love to code.",
}
});
它会工作,已经尝试改变和格式的生日字段,因为我认为这是问题,但同样的错误
1条答案
按热度按时间f3temu5u1#
这一行导致问题:
一条错误消息指示您没有传递数据
缺少data.cpf的参数cpf。
在
route
文件中,这是我们在POST
请求中获取数据的方式: