angularjsnode.jsmongodbangularjs-ng-resource

How can I create a custom query using $resource that passes arguments to the find method in mongoose as req.body?


I want to use a custom query method like this:

 $scope.modules = dataFac.getModules().customQuery({name: /test/})
                    .$promise.then(function(response){
                        $scope.modules = response;
                    },
                    function(response){
                        console.log(response.status);
                    });

In services.js, I have:

dataFac.getModules = function(){
    return $resource(baseUrl+'modules', null, {'update': {method: 'PUT'}, 'customQuery': {method: 'GET'}});
};

In moduleRouter.js, I have:

moduleRouter.route('/')
.get(function(req, res, next){
    Modules.find(req.body, function(err, modules) {
        if (err) {
            console.log(err);
            next(err);
        }
        res.json(modules);
    });
})

Edit: This is passing the {name: /test/} as a query parameter (baseUrl/modules?name=/test/). I had expected it to be passed as req.body, which could have acted as a filter for the Module.find(req.body, callback) method. What am I missing?


Solution

  • You dont have req.body in get method. Just in post ones. Try to use req.query instead.