I am trying to create Singup API in ExpressJS and Prisma.
I am checking whether given email exists in my database or not. When I pass the email and password, it throws Unknown arg email in where.email for type SignupWhereUniqueInput. Did you mean id? Available args: error.
I tried using select: { email: true, password: true } but its not working. Code is working only if I pass id number instead of email. Any ideas what is wrong here??
router.post(`/signup`, async (req, res) => {
const { email, password } = req.body;
const hashPassword = await bcrypt.hash(password, 10);
const checkEmail = await prisma.signup.findUnique({
where: {
email: email
}
});
if (checkEmail) {
return res.status(400).json({
error: "Pick different one"
});
}
const userSignUp = await prisma.signup.create({
data: {
email,
password: hashPassword
}
});
return res.json(userSignUp);
});
// DB Schema
model Signup {
id Int @id @default(autoincrement())
email String
password String
}
The reason you're getting this error is because the email field in your Prisma Signup model is not unique. The where option in a findUnique query only accepts unique fields of a model. This is done to ensure that only one record can be specified by a findUnique query. If you're interested to know more, the Prisma Client API reference explains this behavior in some depth.
To solve the error, make the email field unique inside your Prisma Schema file. The updated Signup model should look like this:
model Signup {
id Int @id @default(autoincrement())
email String @unique
password String
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With