Mysql module executing wrong script - Node Js

0

I'm developing a simple persistence system for customers, products and sales. I made an API using the MySQL expression and module, but something strange is occurring and I really have no idea what it might be:

I have this route:

 app.post("/cliente/salvar", function(request, response){

    let cliente = request.body;

    let connection = dbConnection();

    if(cliente.id){
        clienteFactoryDAO.alterarCliente(connection, cliente, function(erro, result){
            response.send(result);
            if(erro != null) console.log(erro);
            connection.end();
        });
    }else{
        clienteFactoryDAO.salvarCliente(connection, cliente, function(erro, result){
            response.send(result);
            if(erro != null) console.log(erro);
            connection.end();
        });
    }

});

Note that it refers to a method of the clientFactoryDAO module, so this method's code looks like this:

this.salvarCliente = function(connection, objeto, callback){

    let sqlEndereco = utilCliente.createSaveAdressQuery(objeto.endereco);

    console.log(sqlEndereco);

    connection.query(sqlEndereco, function (error, results){
        if(error) throw error;
        objeto.id_endereco = results.insertId;

        //Depois que salva o endereco, usa-se o id persistido e salva-se o cliente
        let sql =  utilCliente.createSaveQuery(objeto);
        console.log(sql);
        connection.query(sql, callback);
    })

}

Note that I close the connection on the second connection.query , inside the callback , this is because I can not close before, since I need to first save the address to that client and then save the client .

Well, the error: The endpoint accessed is correct but at the moment the connection.query() is executed, it executes the product insertion query, I have no idea why! I have read the entire documentation of the MySQL module in GitHub and have not yet succeeded in finding out! Am I closing the connection right? I've tried using the destroy method and nothing!

Thank you in advance and I apologize if I was not clear enough in my doubt.

    
asked by anonymous 15.11.2017 / 19:18

0 answers