Axios doing two post with only one request

Asked

Viewed 20 times

0

Good afternoon! How are you? I am trying to register a user and his restaurant. So I first post the user data and on . then send me to the restaurant registration route. It makes the user’s registration and sends me to this other route, but it returns me a registration error of the user himself, and with this does not end up making the restaurant registration

----------USER POST CODE-----------------

Axios.post('http://localhost:4000/proprietario/cadastro', proprietario.dadosProprietario)
    .then((response) => {
        console.log('Proprietario deu bom')
        console.log(response);
        history.push('/Push/Restaurante');
        
    })
    .catch((error) => {
        console.log('Erro no proprietario')
        console.log(error);
    })

----------------RESTAURANT POST CODE----------------------

 Axios.post('http://localhost:4000/restaurante/cadastro', restaurante.dadosRestaurante)
        .then((response) =>{
            console.log('Restaurante deu boa')
            console.log(response);
            history.push('/Cadastro/Verificacao');
        })
        .catch((error) => {
            console.log('Erro no restaurante');
            console.log(error);
        })

------------------CODE PROPRIETARY REGISTRATION------------------------------

exports.cadastroProprietario = (req, res, next) =>{
mysql.getConnection((error, conn) => {
    if(error) {return res.status(500).send({error: error})};

    conn.query(
        'SELECT * FROM proprietario WHERE email = ?',
        [req.body.email],
        (error, results) => {
            if(error) {return res.status(500).send({error: error})}
            if(results.length > 0){
                res.status(409).send({
                    mensagem: 'Email ja cadastrado!'
                })
            }
            else{
                bcrypt.hash(req.body.senha, 10, (errBcrypt, hash) => {
                    if(errBcrypt){return res.status(500).send({error: errBcrypt})}

                    conn.query(
                        'INSERT INTO proprietario (nome, cnpj, email, senha, telefone) VALUES (?,?,?,?,?)',
                        [req.body.nome, req.body.cnpj, req.body.email, hash, req.body.telefone],
                        (error, result) => {
                            conn.release();
                            if(error){return res.status(500).send({error: error})}

                            const response = {
                                mensagem: 'Proprietario cadastrado'
                            }
                            return res.status(201).send(response);
                        }

                    )
                })
            }
        }
    )
})

}

inserir a descrição da imagem aqui

  • One thing you could do to help better understand the problem would be to debug what happens at this "/owner/registration" address. Error 500 may be occurring by the content sent by the request, must be missing something or must be empty, see what is coming for this route in this case.

  • I understood the problem, but the ideal would be to put the code on the server side. For your case it would be the code that runs when calling the route: /proprietario/cadastro, of the service that is rotating at the door 4000. So we can help figure out the problem.

  • I added the code!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.