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
  • check into console.log(req); if you find your params Commented Apr 10, 2017 at 12:57

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok now its working i change this: controller: var request = $http.post('/movies', { data: { limit: 3 } }); server: var limit = req.body.data.limit;
@MarcinJendruchniewicz if you POST then request.body.X and if GET then request.params.X is used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.