0

I'm using MongoJS in a Node project with Angular and trying to find documents based on a variable.

server.js

app.get('/api/find', function(req, res){

    db.Fruits.find({code:'Apple'}).forEach(function(err, docs){
        res.json(docs);
    });
});

Route

.when('/find/:fruit', {
            templateUrl: 'views/find.html',      
            controller: 'findCtrl',
            resolve: {
                result: function(searchService) {
                    return searchService.getResult();
                }
            }
        })

service.js (client)

.factory('searchService', function($q, $http) {
        return {
            getResult: function() {
                return $http.get('/api/find')
                    .then(function(response) {
                        if (typeof response.data === 'object') {
                            return response.data;
                        } else {
                            return $q.reject(response.data);
                        }
                    })
            }     

        }         
    })

Controller.js

.controller('findCtrl', function($scope, result) {
    $scope.result = result;
})

As you can see in server.js, I'm passing a static string 'Apple'. I wanna replace that with the $routeparams ':fruit' which I've listed in Route.

Please help.

user1915190
  • 307
  • 1
  • 2
  • 14
  • You have `res.json` inside `.forEach` so you're sending response more than once which is what's causing the "can't sent headers again" error – laggingreflex Jul 07 '15 at 11:00
  • @laggingreflex Thank you very much. Could you please also tell me how can I pass a variable to Mogojs? – user1915190 Jul 07 '15 at 11:04

1 Answers1

1

You need to pass the url param :fruit to your searchService:

.when('/find/:fruit', {
    templateUrl: 'views/find.html',      
    controller: 'findCtrl',
    resolve: {
        result: function(searchService) {
            return searchService.getResult($route.current.params.fruit);
        }
    }
})

Search Service:

.factory('searchService', function($q, $http) {
    return {
        getResult: function(fruit) {
            return $http.get('/api/find?fruit=' + fruit)
                .then(function(response) {
                    if (typeof response.data === 'object') {
                        return response.data;
                    } else {
                        return $q.reject(response.data);
                    }
                })
        }     

    }         
})

Server: I normally use mongoose but you could probably use .toArray as using forEach was iterating over the collection and attempting to end the response multiple times.

app.get('/api/find', function(req, res){

    db.Fruits.find({code:req.query.fruit}).toArray(function (err, result) {
        return res.json(result);
    });
});

EDIT: Just read that you're using mongojs You should be able to do:

app.get('/api/find', function(req, res){

    db.Fruits.find({code:req.query.fruit}, function (err, docs) {
        return res.json(docs);
    });
});
itaylorweb
  • 146
  • 5