Hi All, I am currently using the Nextjs App Router and am using yup as my data validation.
I am trying to create a user while testing on postman but I kept receiving this error:
error: PrismaClientValidationError:
Invalid `prisma.user.findUnique()` invocation:
{
where: {
email: undefined,
? id?: Int,
? password?: String,
? name?: String,
? AND?: UserWhereInput | UserWhereInput[],
? OR?: UserWhereInput[],
? NOT?: UserWhereInput | UserWhereInput[],
? role?: EnumRoleNullableListFilter
}
}
Argument `where` of type UserWhereUniqueInput needs at least one of `id`, `email`, `password` or `name` arguments. Available options are listed in green.
at Ur (C:\Users\Harold\OneDrive\Desktop\Course notes\army-cms\nextjs-ssr\node_modules\@prisma\client\runtime\library.js:115:5852)
at Hr.handleRequestError (C:\Users\Harold\OneDrive\Desktop\Course notes\army-cms\nextjs-ssr\node_modules\@prisma\client\runtime\library.js:122:6698)
at Hr.handleAndLogRequestError (C:\Users\Harold\OneDrive\Desktop\Course notes\army-cms\nextjs-ssr\node_modules\@prisma\client\runtime\library.js:122:6388)
at Hr.request (C:\Users\Harold\OneDrive\Desktop\Course notes\army-cms\nextjs-ssr\node_modules\@prisma\client\runtime\library.js:122:6108)
at async l (C:\Users\Harold\OneDrive\Desktop\Course notes\army-cms\nextjs-ssr\node_modules\@prisma\client\runtime\library.js:126:10298)
at async POST (webpack-internal:///(sc_server)/./src/app/api/users/route.js:44:30)
at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:244:37) {
clientVersion: '5.1.1'
}
- error Error: Error creating new user
at POST (webpack-internal:///(sc_server)/./src/app/api/users/route.js:80:15)
at async eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:244:37)
how can I resolve this?
schema as follows:
model User {
id Int @id @default(autoincrement())
email String @unique
password String @unique
name String @unique
role Role[]
}
enum Role {
Admin
Viewer
}
route.js
import { NextResponse } from 'next/server';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
const JWT_SECRET = process.env.JWT_SECRET;
const SALT_ROUNDS = 10;
const Yup = require("yup");
import CreateAdminMailer from "../../../sendgrid/CreateAdminMailer";
import CreateViewerMailer from '../../../sendgrid/createViewerMailer';
export async function POST(req, res) {
const { email, password, name, role } = req.body;
try {
const schema = Yup.object().shape({
email: Yup.string()
.email("Please enter a valid email address")
.required(),
password: Yup.string()
.matches(
/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})/,
"Password must contain: 1 uppercase, 1 lowercase, 1 number, and 1 special character"
)
.required(),
name: Yup.string().required("This field is required"),
role: Yup.string().required("This field is required"),
});
await schema.validate(req.body);
console.log(`check body: ${req.body}`)
const existingUser = await prisma.User.findUnique({
where: {
email: email,
},
});
if (existingUser) {
throw new Error("This account already exists");
} else {
const hashedPassword = await bcrypt.hash(password, SALT_ROUNDS);
const newUser = await prisma.User.create({
data: {
email,
password: hashedPassword,
name,
role,
},
});
if (newUser.role.includes('Admin')) {
await CreateAdminMailer({
email: newUser.email,
});
} else {
await CreateViewerMailer({
email: newUser.email,
});
}
}
return NextResponse.json({ message: `User successfully created: ${newUser}` });
} catch (e) {
if (e instanceof Yup.ValidationError) {
return NextResponse.json({ message: `Yup validation error: ${e}` });
}
console.log("error: ", e);
throw new Error('Error creating new user');
}
};