1

I'm trying to pass the value of an identifier through a url to an express router, the variable I want to pass is IDR that I got it correctly from $routeParams.IDR and now I have to pass it by $http, but I can't do it.

That is my controller.js ( $routeParams.IDR work good)

app.controller("rutaDestinosCtrl", function($scope, $http, userService, $routeParams){      

        vm = this;          
        vm.destinos = [];
        var requestData = {
            'IDR': $routeParams.IDR //$routeParams.IDR is for example: 5
        };


        vm.funciones = {

            obtenerDestinos : function(){                
            $http({
                 method: "GET",
                 url: '/privadas/rutas/obtenerDestinosRuta',
                 requestData,  
                 headers: {'auth-token': userService.token}
            })
                 .then(function(respuesta){
                    vm.destinos = respuesta.data.data;                                      
                }, function(respuesta){
                    console.log("Error:", respuesta.status);
                })   

            } //obtenerDestinos

        }//funciones  

        vm.funciones.obtenerDestinos();
});

After passing the token filter and arriving at this method correctly:

router.get('/obtenerDestinosRuta', function(req,res){

    var query = "SELECT * FROM public.\"Destino\" D " +
            " JOIN public.\"RutaDestino\" RD ON D.\"IDD\" = RD.\"IDD\"" +
            " WHERE \"IDR\" = " + req.body.IDR+ " ORDER BY D.\"IDD\" ASC";

    console.log(query);

    db.query(query).spread(function(result, metadata){
        res.json({
            data: result
        })
    }).catch(function(err){
        res.status(500).send("Error: "+ err);
    })

});

I can't get the value I had stored in RequestData (which would be 5), I tried it several ways without result, in console "req.body.IDR" is undefined, and if I change "req.body.IDR" in the statement by the value 5, works perfectly, any idea of ​​how it can be done?

This it's the query result from the console.log:

SELECT * FROM public."Destino" D JOIN public."RutaDestino" RD ON D."IDD" = RD."IDD" WHERE "IDR" = undefined ORDER BY D."IDD" ASC

Regards.

ElíasMarNev
  • 124
  • 1
  • 14

2 Answers2

2

Can you put also your route definition?

In your route definition you have to specify the expected parameters, as in:

module.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
     when('/routeName/:IDR', {templateUrl: 'template.html', controller: myCtrl})
}]);

Note the ":IDR" value in the route.

Then in your controller you can access it with $routeParams.IDR

zameb
  • 705
  • 11
  • 26
  • Put console.log(JSON.stringify(req)) before the var query='...' instruction. Just to check it is really defined as req.body.IDR and not req.IDR – zameb Aug 21 '17 at 01:17
-1

Thanks guys, finally I find other post with the same problem: angular.http params

But, on resume Angular.http provides an option for send params by $http, called 'params'. That its good if you have to use the method "GET". On my case, after use it, work perfect. That its my code:

On controller.js I have used params and config:

app.controller("rutaDestinosCtrl", function($scope, $http, userService, $routeParams){      
    console.log("Controlador rutaDestinosCtrl");        
        vm = this; 

        vm.destinos = [];

        vm.funciones = {

            obtenerDestinos : function(){                
                var data = {
                    IDR: $routeParams.IDR
                };

                var config = {
                     params: data,
                     headers : {'auth-token': userService.token}
                };

                 $http.get('/privadas/rutas/obtenerDestinosRuta', config)
                 .then(function(respuesta){
                    //Acciones cuando sea una respuesta correcta
                    vm.destinos = respuesta.data.data;                                      
                }, function(respuesta){
                    //Acciones cuando sea una respuesta erronea
                    console.log("Error:", respuesta.status);
                })   

            } //obtenerDestinos

        }//funciones  

        vm.funciones.obtenerDestinos();
});

And to get the value of IDR, I have used req.query.IDR.

router.get('/obtenerDestinosRuta', function(req,res){

    console.log(req.query.IDR);

    var query = "SELECT * FROM public.\"Destino\" D " +
            " JOIN public.\"RutaDestino\" RD ON D.\"IDD\" = RD.\"IDD\"" +
            " WHERE \"IDR\" = " + req.query.IDR + " ORDER BY D.\"IDD\" ASC";

    console.log(query);

    db.query(query).spread(function(result, metadata){
        res.json({
            data: result
        })
    }).catch(function(err){
        res.status(500).send("Error: "+ err);
    })

});
ElíasMarNev
  • 124
  • 1
  • 14