I’m building an app with node.js knex.js typescript zod and fastify, im trying to validate the emails so that when an email is inserted on the user registration, if it is a duplicate it throws status 401(conflict), but even if the emails does’nt exist it’s generating the status, i can’t see why it isn’t working.
export async function userRegister(
request: FastifyRequest,
reply: FastifyReply,
) {
const registerBodySchema = z.object({
name: z.string(),
email: z.string().email(),
password: z.string().min(6),
});
const { name, email, password} = registerBodySchema.parse(request.body);
const password_hash = await hashSync(password, 6);
const userWithSameEmail = await knex.select('*').from('users').where('email' , email)
if (userWithSameEmail){
return reply.status(409).send({userWithSameEmail});
}
await knex('usuarios').insert({
id: crypto.randomUUID(),
nome,
email,
password: password_hash,
});
return reply.status(201).send();
}
i tried using getting the email from the table using knex and it responses fine when i test it, the duplicate email shows on the logs
export async function userRegister(
request: FastifyRequest,
reply: FastifyReply,
) {
const registerBodySchema = z.object({
name: z.string(),
email: z.string().email(),
password: z.string().min(6),
});
const { name, email, password} = registerBodySchema.parse(request.body);
const password_hash = await hashSync(password, 6);
const userWithSameEmail = await knex.select('*').from('users').where('email' , email)
response { userWithSameEmail }
if (userWithSameEmail){
return reply.status(409).send();
}
await knex('usuarios').insert({
id: crypto.randomUUID(),
nome,
email,
password: password_hash,
});
return reply.status(201).send();
But when i remove the response{ userWithSameEmail } and let the code run it only returns status(401) even if the email isn’t a duplicate, can someone help?
Lucas Faria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.