I'm encountering an issue with Prisma while trying to update a user's password in my Node.js application. Whenever I attempt to change the password using the changePassword function in my authController.js file, I receive the following error:
PrismaClientValidationError: Invalid `prisma.user.findUnique()` invocation:
Argument `where` of type UserWhereUniqueInput needs at least one of `id` or `email` arguments.
Here's the relevant code snippet from my authController.js file:
const changePassword = async (req, res) => {
// #swagger.tags = ['Authentication']
/* #swagger.security = [{
"bearerAuth": []
}] */
const { userId } = req.user; // Extracted from JWT token in auth middleware
const { oldPassword, newPassword } = req.body;
try {
// Fetch user from database using userId
const user = await prisma.user.findUnique({ where: { id: userId } });
if (!user) {
return res.status(404).json({ message: "User not found" });
}
// Validate old password
const isMatch = await bcrypt.compare(oldPassword, user.password);
if (!isMatch) {
return res.status(400).json({ message: "Incorrect old password" });
}
// Hash new password
const salt = await bcrypt.genSalt(10);
const hashedPassword = await bcrypt.hash(newPassword, salt);
// Update user's password
await prisma.user.update({
where: { id: userId },
data: { password: hashedPassword },
});
res.json({ message: "Password updated successfully" });
} catch (error) {
console.error("Error changing password:", error);
res.status(500).json({ message: "Server error" });
}
};
and this is verifyToken middleware:
const jwt = require('jsonwebtoken');
const verifyToken = (req, res, next) => {
const authHeader = req.header('Authorization');
if (!authHeader) return res.status(401).json({ error: true, message: 'Authorization header missing' });
const token = authHeader.split(" ")[1];
if (!token) return res.status(401).json({ error: true, message: 'Token not provided' });
jwt.verify(token, process.env.JWT_SECRET, (err, user) => {
if (err) {
console.error('JWT verification error:', err);
return res.status(403).json({ error: true, message: 'Invalid token' });
}
req.user = user;
next();
});
};
module.exports = verifyToken
I'm not sure why I'm getting this error since I'm providing the id argument in the where clause when calling prisma.user.findUnique. Any help on resolving this issue would be greatly appreciated. Thank you!
this is the error:
try {
// Fetch user from database
→. const user = await prisma.user.findUnique({
where: {
id: undefined,
? email?: String,
? AND?: UserWhereInput | UserWhereInput[],
? OR?: UserWhereInput[],
? NOT?: UserWhereInput | UserWhereInput[],
? username?: StringFilter | String,
? password?: StringFilter | String,
? createdAt?: DateTimeFilter | DateTime,
? updatedAt?: DateTimeFilter | DateTime,
? role?: EnumRoleFilter | Role,
? profile?: ProfileNullableRelationFilter | ProfileWhereInput | Null,
? review?: ReviewListRelationFilter
},
select: {
email: true,
username: true,
password: true,
createdAt: true,
updatedAt: true,
role: true,
profile: true
}
})
Argument `where` of type UserWhereUniqueInput needs at least one of `id` or `email` arguments. Available options are marked with ?.
The error Prisma is giving you is:
This means that Prisma is not receiving either the
idoremailargument in theprisma.user.findUniquecall. This probably means thatreq.user.userIdis not present within yourchangePasswordfunction. I would start debugging there to guarantee thatreq.user.userIdexists before passing it to Prisma, because Prisma won't work as intended until you do.