Error saving multiple select

0

I have this ajax

$(function() {
    $('#asignarConteo').on('click', function(e) {
        $('.widthSelect :selected[value!="0"]').closest("tr").each(function() {

            var ar = $(this).find(".iarticulo").text();
            var usu = $(this).find(":selected").text();
            //Disabled select seleccionado.

            $.ajax({
                url: 'http://localhost:3000/AsigUsuarios',
                method: 'post',
                data: { idArticulo: ar, Usuario: usu },
                success: function(res) {
                    console.log(res);
                    console.log('Entro');
                    //Disabled select seleccionado.
                    $(".widthSelect").filter(function() {
                        return this.selectedIndex > 0;
                    }).prop('disabled', true);


                }
            });
        });
    });
});

and this is the query that inserts me into the database.

 postAsignarUsuario: function(req, res, next) {
        console.log('Entra a postAsignarUsuario');
        var id = req.body.idArticulo;
        var usuario = req.body.Usuario;
        var config = require('.././database/config');
        console.log(id);
        var respuesta = { res: false };
        sql.connect(config)
            .then(function() {
                var request11 = new sql.Request();
                request11.query("INSERT INTO SI_Conteo(SI_Num_Inventario,SI_Ubicacion,SI_Ubicacion_Fisica,SI_Num_Articulo, SI_Cantidad,SI_Num_Conteo,SI_Fecha_Conteo,SI_Usuario,SI_OV) SELECT SI_Num_Inventario = COALESCE (t.SI_Num_Inventario,c.SI_Num_Inventario),(CASE WHEN c.SI_OV IS NULL OR c.SI_OV = '' THEN  COALESCE(t.SI_Ubicacion, c.SI_Ubicacion) ELSE NULL END)AS SI_Ubicacion, (CASE WHEN c.SI_OV IS NOT NULL THEN  COALESCE(t.SI_Ubicacion_Fisica, c.SI_Ubicacion_Fisica) ELSE NULL END)AS SI_Ubicacion_Fisica,SI_Num_Articulo = COALESCE(t.SI_Articulo, c.SI_Num_Articulo),NULL, SI_Num_Conteo = COALESCE(cs.SI_Num_Conteo,2),GETDATE(),'" + req.body.Usuario + "',c.SI_OV FROM SI_Inventario_Teorico_QAD t full JOIN SI_Conteo c ON t.SI_Articulo = c.SI_Num_Articulo AND t.SI_Ubicacion = c.SI_Ubicacion INNER JOIN SI_Maestro_Ref_QAD m ON  t.SI_Articulo = m.SI_Num_Articulo OR c.SI_Num_Articulo = m.SI_Num_Articulo FULL JOIN SI_Consecutivo cs ON  c.SI_Num_Inventario = cs.SI_Num_Inventario AND cs.SI_Estado = 0 WHERE c.SI_Num_Articulo ='" + req.body.idArticulo + "' OR t.SI_Articulo = '" + req.body.idArticulo + "'")
                    .then(function(recordset) {
                        console.log('Recordset: ' + recordset);
                        console.log('Affected: ' + request11.rowsAffected);

                        respuesta.res = true;
                        sql.close();
                        res.json(respuesta);
                        //sql.close();

                    })
                    .catch(function(err) {
                        console.log('Request error: ' + err);
                    });
            })
            .catch(function(err) {
                if (err) {
                    console.log('SQL Connection Error: ' + err);
                }
            });
    }

Right now I have 2 problems.

  • If I choose more than 1 select to insert in the database it tells me the following error

    POST http://localhost:3000/AsigUsuarios 500 (Internal Server Error)
    Object {res: true}
    

    that is, only the first one is inserting me. and the error tells me:

    Global connection already exists. Call sql.close() first.
    
  • When I save an article once and the select becomes disabled and if I want to save another one it returns and it saves me the one that is already selected.

  • asked by Eduard 27.07.2017 в 15:58
    source

    0 answers