Registration of MySQL records with NodeJS (Backend) that does not enter "Success" and hangs. That could be happening?

0

I expose the code to see if someone can help me

HTML code with the button that calls the function:

<div  id="btn_crearcurs" class="btn_curs">
     <a class="btn btn-warning btn-lg btn-block" ng-click="crearCursos()">Crear cursos i aules</a>
</div>

AngularJS driver that passes the data to NodeJS:

$scope.crearCursos = function () {
     $scope.loader.loading = true;
     var lista = $scope.selected;
     if (lista.length <1)
     {
         alert("No ha seleccionat ningún curs.");
     }
     else
     {
         $scope.loader.loading = true;
         $http.post("/cursos/cursosC", {
             'seleccio' : $scope.selected,
             'ip' : $rootScope.mi_ip,
             'usu' : $rootScope.idUsu,
             'cif' : $rootScope.cifAssoc
         })
         .success(function (data, status, headers, config) {
             $scope.loader.loading = false;                    
             $state.go("private.adminsAssoccurs");
         })
         .error(function (error) {
             $scope.loader.loading = false;
             console.log(error);
         });                
         $state.go("private.adminsAssoccurs");
     }
};

Code (path) in NodeJS that inserts the records in the MySQL database:

app.post('/cursos/cursosC', function (req, res) {
    var seleccio = req.body.seleccio;
    var ip = req.body.ip;
    var usuari = req.body.usu;
    var cif = req.body.cif;
    var valores = preparaMatriz(seleccio, ip, usuari, cif);
    db.get().query("INSERT INTO cursoscentres (cifAssocCursAssoc, cursAssoc, descripcioCursAssoc, aulaCursAssoc, datacreacioCursAssoc, ipcreacioCursAssoc, usuaricreacioCursAssoc, altaCursAssoc) VALUES ?", [valores], function (err, result) {
        if (err) {
            var data = {"error": 1, "err": err};
            return data;
        } else {
            var data = {"error": 0};
            //console.log(data);
            return data;
        }
    });
});
    
asked by Carlos 16.08.2017 в 08:56
source

1 answer

0

As the administrator says, your question is a little vague, but I would dare to say and that's why I answer you, that your problem is, in the node part, you do not return the request, the post receives' req 'and' res' (in addition to 'next' as third parameter if you need it), after doing the insert you should do something like:

res.send(data);

This causes express to terminate the request that has been made and return the response to the front, read a bit in the api of express on the Response object. But come on, it looks like that's your problem.

    
answered by 20.08.2017 в 21:28