Http Post sends the data in disarray

2

I have a function that sends data that is in a vector with an http post, so what I do is go through the vector with a for cycle and I send the data to the database, the problem I have is that the data is arriving in disorder to the database, that is, first the position 2 of the vector rises, then the 4, and thus, that is, they are rising in a random way. This is the code.

$scope.enviarlimites = function (){
    var nombreLimite = document.getElementById('nombre').value;
    var idMapa = document.getElementById('mapa_id').value;

    if($scope.numerodepuntos==0 || $scope.numerodepuntos==null || nombreLimite==null || nombreLimite==""){
        swal({
            title: "Error",
            text: "Debe ingresar todos los datos del mapa.",
            icon: "error"
        })
    }else{
        for(var i=0; i<$scope.numerodepuntos; i++){

            var latitudbd = $scope.vectorCoordenadas[i].lat;
            var longitudbd =  $scope.vectorCoordenadas[i].lng;
            var nombrelimitebd = nombreLimite+i;

            $http.post("/territoriosinteligentes/enviarlimites", {nombre: nombrelimitebd, latitud: latitudbd,
                longitud: longitudbd, identificador: nombreLimite, mapa_id: idMapa}).then(function(response)
            {

                $scope.status = response.status;
                $scope.data = response.data;
                swal({title: "Cargando Limites",text: "Por favor espere.",icon: "success"})

            },

            function(response) {
                $scope.data = response.data || 'Request failed';
                $scope.status = response.status;
                swal({
                    title: "Error",
                    text: "No se pudo hacer el registro de los limites, debe ingresar todos los datos",
                    icon: "error"
                })
            })
        }
    }
}
    
asked by Talked 17.12.2018 в 22:34
source

1 answer

0

The most practical thing would be to send the whole point vector in a single call, with this the logic of the frontend is simplified a lot, and the responsibility is passed to the backend, which would make the app more performant since you do not have n calls to the API, the calls are expensive, I'll give you an example of how you could solve the front part

$scope.enviarlimites = function (){
    var nombreLimite = document.getElementById('nombre').value;
    var idMapa = document.getElementById('mapa_id').value;

    if($scope.numerodepuntos==0 || $scope.numerodepuntos==null || nombreLimite==null || nombreLimite==""){
        swal({
            title: "Error",
            text: "Debe ingresar todos los datos del mapa.",
            icon: "error"
        })
    }else{
        $http.post("/territoriosinteligentes/enviarlimites", { 
            puntos: $scope.vectorCoordenadas, 
            identificador: nombreLimite, 
            mapa_id: idMapa })
        .then(function(response) {
                $scope.status = response.status;
                $scope.data = response.data;
                swal({title: "Cargando Limites",text: "Por favor espere.",icon: "success"})
            },

            function(response) {
                $scope.data = response.data || 'Request failed';
                $scope.status = response.status;
                swal({
                    title: "Error",
                    text: "No se pudo hacer el registro de los limites, debe ingresar todos los datos",
                    icon: "error"
                })
            }
        )
    }
}
    
answered by 18.12.2018 в 15:01