Home > Enterprise >  Cannot send Post Request Again after status 401 error even after correcting the request
Cannot send Post Request Again after status 401 error even after correcting the request

Time:03-22

I have a registration page. I am adding new user to a postgresql db. If I add a user with an existing email and submit a post request to Nodejs (express) REST API. I have validation and it returns status 400 which is right. But the problem is that without reloading the page when I correct the name field and resend it, nothing is shown in the network nothing happened. This is my Submit funtion:

const onSubmit = (values, { resetForm, setStatus }) => {
    axios
        .post("http://localhost:3006/api/inscrire",{values})
        .then((response) => {
            resetForm({ values: "" })
            setStatus({ success: "Email sent !" })
            navigate("/", { state: 'hello from signup page' })
        })
        .catch((err) => {
            console.log(err.response.data);
        });
};

and here is the controller (backend side )

const Register = (req, res, next) => {
bcrypt.hash(req.body.values.motdepass1, 10)
    .then((hash) => {
        const newUser = pool.query("INSERT INTO public.user(nom, prenom, email, numtelephone, numfixe, naissance, sexe, motdepass, status, nationalite) VALUES($1, $2,  $3, $4, $5, $6, $7, $8, $9, $10) RETURNING _id", [
            req.body.values.nom,
            req.body.values.prenom,
            req.body.values.email,
            req.body.values.tel,
            req.body.values.tell,
            req.body.values.naissance,
            req.body.values.sexe,
            hash,
            req.body.values.status,
            req.body.values.nationalite,
        ])
            .then((result) => {
                return res.status(201).json({ message: "Utilisateur créer avec succés !" })
            })
            .catch(err => {
                if (err.code === '23505') {
                    res.status(401).json({ message: "Veuillez utiliser un autre e-mail !" })
                } else {
                    res.status(401).json({ message: "erreur Interne 401" })
                }
            })
    })
    .catch((err) =>
        res.status(400).json({ err }
        ))

}

i'm using Formik to manage my forms

CodePudding user response:

Just added setSubmitting(false) in the catch block and everything is fine

  • Related