I have put a UNIQUE constraint on the "email" column of my table and now in my Express API which is using Node-Postgres (pg), I want to make sure than the email that the user puts in when POSTing a student, isn't duplicated.
My question is that how can I show a response like "Email already taken!" in my JSON object when that constraint gets violated?
const createStudent = (req, res) => {
const { name, email, age, dob } = req.body;
pool.query(insertStudent, [name, email, age, dob], (error, results) => {
if (error) throw error;
res.status(201).json({
message: `Student Created Successfully!`,
student: results.rows[0],
});
});
};