When ever i send the data from the postman, I always encounter this error. It said hash and data is required even tho i’ve already put passwords and also awaits.
This is my user controller.
import { User } from "../models/user.models.js";
import asyncHandler from "express-async-handler";
const registerUser = asyncHandler(async (req, res) => {
try {
const { username, email, password } = req.body;
if ([username, email, password].some((fields) => fields === undefined)) {
return res.status(400).send({ message: "All fields are required" });
}
if (password.length < 6) {
return res
.status(400)
.send({ message: "Password must be atleast 6 character" });
}
if (!email?.includes("@")) {
return res.status(400).send({ message: "Invalid email" });
}
const isUserExists = await User.findOne({
$or: [{ username }, { email }],
}).select("-password");
if (isUserExists) {
return res
.status(400)
.send({ message: "Username or email already taken." });
}
const user = await User.create({
username,
email,
password,
});
if (!user) {
return res.status(500).send({ message: "User not created" });
}
return res
.status(200)
.send({ user, message: "User successfully registered" });
} catch (error) {
return res.status(500).send({
message: "User registration unsuccessful: ",
error: error.message,
});
}
});
const loginUser = asyncHandler(async (req, res) => {
try {
const { username, email, password } = req.body;
if (!username && !email) {
return res.status(400).send({ message: "Username or email is required" });
}
if (!password) {
return res.status(400).send({ message: "Password is required" });
}
const user = await User.findOne({ $or: [{ username }, { email }] }).select(
"-password"
);
if (!user) {
return res
.status(400)
.send({ message: "User with this username or email doesn't exists" });
}
const passwordCheck = await user.isPasswordCorrect(password);
if (passwordCheck === false) {
return res.status(400).send({ message: "Invalid credentials" });
}
const token = await user.generateToken();
if (!token) {
return res
.status(500)
.send({ message: "Error while generating access token" });
}
return res
.status(200)
.cookie("token", token)
.send({ user, message: "User successfully logged in" });
} catch (error) {
return res
.status(400)
.send({ message: "Login unsuccessful", error: error.message });
}
});
export { registerUser, loginUser };
and this is my usermodel
import mongoose, { Schema } from "mongoose";
import bcrypt from "bcrypt";
import jwt from "jsonwebtoken";
const userSchema = new Schema(
{
username: {
type: String,
required: true,
lowercase: true,
unique: true,
},
email: {
type: String,
required: true,
lowercase: true,
unique: true,
},
password: {
type: String,
required: true,
min: 6,
},
},
{ timestamps: true }
);
userSchema.pre("save", async function (next) {
try {
return await bcrypt.hash(password, this.password);
} catch (error) {
console.log("Bcrypt error: ", error);
return false; // Return false if an error occurs
}
});
userSchema.methods.isPasswordCorrect = async function (password) {
return await bcrypt.compare(password, this.password);
};
userSchema.methods.generateToken = async function () {
return jwt.sign({ id: this._id }, process.env.ACCESSTOKENSECRET, {
expiresIn: "30d",
});
};
export const User = mongoose.model("User", userSchema);
I tried googling the error and also asked chatgpt. But it doesn’t help. I’ve also added console.log in every single function. The error is in await user.isPasswordCorrect(password) line. Also there are simalar questions as mine but none of that answers work for me.Thank you!