0

Hi everyone i have a problem with passing value from angular to node... I want get limit value on server and pass it to limit

this is my Server:

app.get("/movies", function(req, res) {

var limit = req.query.limit; // i can't get this (variable is undefined)
console.log(limit);

MongoClient.connect(dbUrl, function(err, db) {
    if (err) {
        res.status(500);
        res.json({ error: true });

        return;
    }

    db.collection("movies").find({}, { limit: limit }).toArray(function(err, docs) {

        if (err) {
            res.status(500);
            res.json({ error: true });

            return;
        }

        res.json(docs);

        db.close();
    });

});

});

Controller

Ctrls.controller('movies', ['$scope', "$http", "$location", function($scope, $http, $location) {


    $scope.data = [];
    var request = $http.get('/movies', {
        limit: 2
    });
    request.success(function(data) {
        $scope.movies = data;
        console.log(data);

    });
    request.error(function(data) {
        console.log('Error: ' + data);
    });


}]);

I want get the value 'limit' in node, what i do wrong ? In server variable is undefined (x2)

thanks

1 Answers1

0

change this:

var limit = req.query.limit; // i can't get this (variable is undefined)

to:

var limit = req.params.limit;

or:

var limit = req.body.limit;

That is because by default angular sends data in the request-payload not in form-data.

Jai
  • 74,255
  • 12
  • 74
  • 103
  • Ok now its working i change this: controller: var request = $http.post('/movies', { data: { limit: 3 } }); server: var limit = req.body.data.limit; – Marcin Jendruchniewicz Apr 10 '17 at 13:20
  • @MarcinJendruchniewicz if you POST then `request.body.X` and if GET then `request.params.X` is used. – Jai Apr 11 '17 at 04:44